Programming Assignment #72
Code
/// Name: Dylan Sleeper
/// Period: 6
/// Program Name: guess
/// File Name: Guessing.java
/// Date Finished: 1/5/2016
import java.util.Random;
import java.util.Scanner;
public class Guessing{
public static void main(String[] args){
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int num = r.nextInt(10) + 1;
int guess;
int tries = 0;
System.out.println("I have chosen a number 1-10. Try to guess it");
do{
System.out.print("Your guess: ");
guess = keyboard.nextInt();
if(guess != num){
System.out.println("That's incorrect. Guess again.");
}
tries++;
} while(guess != num);
System.out.println("You got it! It only took " + tries + " tries!");
}
}
Outputs