Assignment #72 Again with the Number-Guessing
Code
/// Name: Koosha Kimelman
/// Period: 7
/// Program Name: Again with the Number-Guessing
/// File Name: NumberGuessingAgain.java
/// Date Completed: 11/17/15
import java.util.Scanner;
import java.util.Random;
public class NumberGuessingAgain {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int guess, answer, tries;
answer = (1+r.nextInt(10));
tries = 0;
System.out.println("I have chosen a number between 1 and 10. Try to guess it.");
do {
System.out.print("Your guess: ");
guess = keyboard.nextInt();
tries++;
if (guess != answer)
System.out.println("That is wrong. Try again");
} while (guess != answer);
System.out.println("That's correct. It was " + answer + ". It took you " + tries + " tries to get it.");
}
}
Picture of the output