Assignemnt #67: Adding Values in a Loop
Code
///Name: Cassie
///Period: 6
///Program Name: Adding Values in a Loop
///File Name: loopvalues.java
///Date Finished: 1/11/16
import java.util.Random;
import java.util.Scanner;
class loopvalues
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int guess, guesses=0, ans = 1 + r.nextInt(100);
System.out.print( "Guess a number between 1-100 " );
guess = keyboard.nextInt();
while (guess != ans && guesses < 6 )
{
if ( guess < ans )
System.out.println( "Too low. Guess again " );
else if ( guess > ans )
System.out.println( "Too high. Guess again " );
guesses++;
guess = keyboard.nextInt();
}
if ( guesses == 6 && guess != ans )
System.out.println( "You have exceeded your number of guesses" );
else
System.out.println( "Nice you got it" );
}
}
Picture of the output