While Loops #65 (Well... that was easy!)
Code
/// Name: Amir Salehi
/// Period: 7
/// Program Name:Number Guess Counter
/// File Name:NumberGuessCounter.java
/// Date Finished: 12/8/2015
import java.util.Random;
import java.util.Scanner;
public class NumberGuessCounter
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner input = new Scanner(System.in);
int x = 1 + r.nextInt(10), guess;
int count = 0;
System.out.print("I'm thinking of a number from 0 - 10. What is it? ");
System.out.print("Your guess: ");
guess = input.nextInt();
count++;
while (guess != x)
{
System.out.print("");
System.out.println(" This is incorrect. Guess again.");
System.out.print("Your guess: ");
guess = input.nextInt();
count++;
}
if (guess == x) {
System.out.println("That's right! My secret number was " + x + "!");
System.out.println("It only took you " + count + " tries.");
}
}
}
Picture(s) of the output