Assignment #61: Keep Guessing
Code
/// Name: Kiran O'Farrell
/// Program Name: Number Guesser II
/// File Name: NumberGuesserII.java
/// Date Completed: 1/6/16
import java.util.Random;
import java.util.Scanner;
public class NumberGuesserII {
public static void main (String[] args) {
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int secret = 1 + r.nextInt(10);
int guess;
System.out.println("Try to guess the secret number!");
guess = keyboard.nextInt();
while (secret != guess) {
System.out.println("Sorry! You guessed wrong.");
guess = keyboard.nextInt();
}
if (guess == secret) {
System.out.println("Great job! You guessed the right number!");
}
else {
System.out.println("Sorry! You guessed the wrong number.");
}
}
}
Picture of the output