Write a Java Program according to the following description: Two people play the
ID: 3767192 • Letter: W
Question
Write a Java Program according to the following description:
Two people play the game of Count 21 by taking turns entering a 1, 2, or 3, which is
added to a running total. The player who adds the value that makes the total exceed
21 loses the game. Create a game of Count 21 in which a player competes against the
computer, and program a strategy that always allows the computer to win. On any
turn, if the player enters a value other than 1, 2, or 3, force the player to reenter the
value. Save the game as Count21.java.
Explanation / Answer
Answer :
import java.util.*;
public class Count21{
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int number = random.nextInt(21);
int guess = -1;
while (guess!=number) {
System.out.print("Enter guess number: ");
guess = scanner.nextInt();
if (guess<number) {
System.out.println("Guess is too low, please try again");
} else if (guess>number) {
System.out.println("Guess is too high, please try again");
} else {
System.out.println("Guess is Correct, the number was " + number);
}
}
}
}