Assignemnt #65 Number-Guessing with a Counter
Code
/// Name: Jake Johnson
/// Period: 7
/// Program Name: MoreGuessing
/// File Name: MoreGuessing.java
/// Date Finished: 12/8/2015
import java.util.Scanner;
import java.util.Random;
public class MoreGuessing
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int secret, guess;
int tries = 1;
System.out.println("I have chosen a number between 1 and 10. Try to guess it.");
System.out.print("Your guess: ");
guess = keyboard.nextInt();
secret = 1 + r.nextInt(10);
while (guess != secret)
{
System.out.println("\nNope, guess again.");
System.out.print("Your guess: ");
guess = keyboard.nextInt();
tries++;
}
System.out.println("\nYep, it was " + secret + ".");
System.out.println("Yep it only took " + tries + " tries.");
}
}
Picture of the output