Assingment #72: Again with the Number Guessing
Code
/// Name: Kiran O'Farrell
/// Program Name: NumberGuesserIV
/// File Name: NumberGuesserIV.java
/// Date Completed: 1/20/16
import java.util.Random;
import java.util.Scanner;
public class NumberGuesserIV {
public static void main (String[] args) {
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int secret = 1 + r.nextInt(10);
int guess;
int attempts = 0;
System.out.println("Try to guess the secret number!");
guess = keyboard.nextInt();
do {
System.out.println("Sorry! You guessed wrong.");
attempts++;
guess = keyboard.nextInt();
} while (secret != guess);
if (guess == secret) {
System.out.println("Great job! You guessed the right number! You got it in " + attempts + " guesses.");
}
else {
System.out.println("Sorry! You guessed the wrong number.");
}
}
}
Picture of the output