Assignemnt #65
Code
/// Name: Sam Menar
/// Period: 6
/// Program Name: NumberGuessingGame
/// File Name: NumberGuessingGame.java
/// Date Finished: 12/16/15
import java.util.Random;
import java.util.Scanner;
public class NumberGuessingGame
{
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.println(" ");
System.out.print("Your guess: ");
guess = keyboard.nextInt();
count++;
while ( guess != choice )
{
System.out.println("Sorry, thats incorrect. Guess again. ");
System.out.print("Your guess: ");
guess = keyboard.nextInt();
count++;
}
if ( guess == choice )
{
System.out.println("Congradulations you got it right!");
System.out.println("It only took you " + count + " tries!");
}
}
}
Picture of the output