Assignment #65 and NumberGuessingWithCounter
Code
/// Name: Jason Kim
/// Period: 7
/// Program Name: NumberGuessingWithCounter
/// File Name: NumberGuessingWithCounter.java
/// Date Finished:11/5/15
import java.util.Scanner;
import java.util.Random;
public class NumberGuessingWithCounter
{
public static void main (String[] args)
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int secretNumber = 1 + r.nextInt(10), number, counter = 0;
System.out.println("I have chosen a number from 1 to 10. Try to guess it.");
System.out.print("Your guess: ");
number = keyboard.nextInt();
counter++;
while ( number != secretNumber)
{
System.out.println("That is incorrect. Guess again.");
System.out.print("Your guess: ");
number = keyboard.nextInt();
counter++;
}
System.out.println("That's right! You're a good guesser.");
System.out.println("It only took you " + counter + " tries.");
}
}
Picture of the output