While Loops #61 (Guessing game with multiple tries!)

Code

/// Name: Amir Salehi
/// Period: 7
/// Program Name:KeepGuessing
/// File Name:KeepGuessing.java
/// Date Finished: 12/4/2015

import java.util.Random;
import java.util.Scanner;

public class KeepGuessing
{
	public static void main ( String[] args )
	{
		Random r = new Random();
        
        Scanner input = new Scanner(System.in);
        
        int x = 1 + r.nextInt(10);
        

        System.out.print("I'm thinking of a number from 0 - 10. What is it? ");
        int guess = input.nextInt();

        while (guess != x) {
            System.out.println("Incorrect, guess again!");
            guess = input.nextInt();
        }
            System.out.println("That's right! You're a good guesser.\n");
    }
}
    

Picture(s) of the output

Assignment 7

Back to Homepage