/* In the lesson we will practise using the basic Java tools learned in previous articles. To do it let's develop the "Guess game". Its rules are as follows:
Computer proposes a number from 1 to 10.
Human player tries to guess it. One enters a guess and computer tells if the number matches or it is smaller/greater than the proposed one.
Game continues, until player guesses the number. */
import java.util.Scanner;
public class NumberGuessingGame {
public static void main(String[] args) {
int secretNumber;
secretNumber = (int) (Math.random() * 10 + 1);
System.out.println("Secret number is " + secretNumber); // to be removed
// later
Scanner keyboard = new Scanner(System.in);
int guess;
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
if (guess == secretNumber)
System.out.println("Your guess is correct. Congratulations!");
else if (guess < secretNumber)
System.out
.println("Your guess is smaller than the secret number.");
else if (guess > secretNumber)
System.out
.println("Your guess is greater than the secret number.");
}
}
No comments:
Post a Comment