Java Guessing Game Help?
I've been asked to create a guessing game in Java based on the following system requirements:
Design and develop an application system that will allow the user to guess a ‘secret
number’ in the range of 1 to 10.
There are two modes of play: Test and Normal. In the ‘Normal’ mode, the secret number
is generated by the system. In the ‘Test’ mode, the system asks the user to enter a number as the secret number
The user interface should be text-based. The system first asks the user to choose the mode, e.g.
System displays:
“Select the mode of play: 1 Normal; 2 Testing”
The user would enter 1 or 2.
After choosing the mode, the main menu is as follows:
1. Start a new game
2. Show the Stats
3. Exit
When choice ‘1. Start a new game’ is selected from the main menu, the system will ask the user to enter the number of lives, i.e. the number of attempts that a user is allowed to guess. If the mode selected is ‘Test’, the system will also ask the user to enter the secret number. Then, the user can start guessing the secret number by entering the guesses.
After each guess, the system displays one of the following messages:
• “You won” if the guess is correct;
• “Higher! You lost” if the secret number is higher than the guess, and the user has run out of lives;
• “Lower! You lost” if the secret number is lower than the guess, and the user has run out of lives;
• “Higher!, Try again” if the secret number is higher than the number entered, and the user still has lives left;
• “Lower! Try again” if the secret number is lower than the guess, and the user still has lives left.
- 3 -
After winning or losing each game, the system will get back to the main menu until ‘3.
Exit’ is selected from the main menu.
If choice ‘2. Show the Stats’ is selected from the main menu, the system displays the percentage of games won, i.e. the result of ‘games won’ divided by ‘total number of games played’ during the execution of your program. There is no need to address persistency.
I've started off with a program to guess the number between 1 and 10 however, I don't know how to go about intergrating the 'lives' into the program as specified. Any advice would be much apreicated.
MY CODE SO FAR
Code:
import java.util.Random;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
int secretNum, guess;
Scanner IO = new Scanner(System.in);
secretNum = (new Random()).nextInt(10) + 1;
boolean isWrong = true;
while (isWrong) {
System.out.println("Select the mode of play: 1 Normal; 2 Testing");
System.out.println("Guess a number between 1 and 10: ");
guess = IO.nextInt();
if (secretNum < guess) {
System.out.println(" Your guess is to high ! Try Again ! ");
} else if (secretNum > guess) {
System.out.println(" Your guess is to low ! Try Again ! ");
} else {
System.out.println(" Your guess was correct ! ");
isWrong = false;
}
}
}
}