Assignment #65 and Number Guessing With counting
Code
///Name: Tim Gibson
///Period: 6
///Project Name: Number Guessing with counting
///File Name: NumberGuessing3.java
///Date Finished: 10/20/15
import java.util.Scanner;
import java.util.Random;
public class NumberGuessing3
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int num, guess, n;
n = 0;
num = 1 + r.nextInt(10);
System.out.print( "I'm thinking of a number from 1 to 10.\nYour Guess: " );
guess = keyboard.nextInt();
n++;
while ( guess != num )
{
System.out.print( "Sorry, that wasn't the number. Guess again.\nYour Guess: " );
guess = keyboard.nextInt();
n++;
}
System.out.println( "\nThat's right, my secret number was " + num + "!\nIt only took you " + n + " tries." );
}
}
Picture of the output