Assignemnt #65 and Number Guessing with a Counter
Code
/// Name: David Shkolnikov
/// Period: 6
/// Program Name: Number Guessing
/// File Name: NumberGuessing.java
/// Date Finished: 12/16/2015
import java.util.Random;
import java.util.Scanner;
public class NumberGuessing
{
public static void main( String[] args )
{
Random r = new Random();
int choice = 1 + r.nextInt(10);
int guess, count = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("I'm thinking of a number from 1 to 10.");
System.out.print("Your guess: ");
guess = keyboard.nextInt();
System.out.println(" ");
count++;
while ( choice != guess )
{
System.out.println("That is incorrect. Guess again.");
System.out.print("Your guess: ");
guess = keyboard.nextInt();
count++;
}
if ( choice == guess )
{
System.out.println("That's right! You're a good guesser.");
System.out.println("It only took you " + count + " tries.");
}
}
}
Picture of the output