Do-While Loops #72 (... Simple)
Code
/// Name: Amir Salehi
/// Period: 7
/// Program Name:Again Number Guessing Counter
/// File Name:AgainNumberGuessingCounter.java
/// Date Finished: 12/17/2015
import java.util.Random;
import java.util.Scanner;
public class AgainNumberGuessingCounter
{
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++;
do
{
System.out.print("");
System.out.println(" This is incorrect. Guess again.");
System.out.print("Your guess: ");
guess = input.nextInt();
count++;
} while (guess != x);
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