Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class NumberGame
- {
- public static void main (String[] args)
- {
- //Initial print statement. Prompts the user for his/her name
- System.out.println("Hello! Please begin by entering your name: ");
- //Creates a Scanner to allow the user to enter their name
- Scanner scan1 = new Scanner (System.in);
- String name = scan1.nextLine();
- //Explains the game to the user. Instructions are broken up into smaller sections.
- System.out.println("Thanks " + name + "! Let's begin by explaining the game.");
- System.out.println();
- System.out.println("The object of the game is to correctly guess the secret number.");
- System.out.println("The number is randomly generated, and is different every time you play.");
- System.out.println();
- System.out.println("***Press ENTER to Continue***");
- scan1.nextLine();
- System.out.println("There are three levels, each harder than the last.");
- System.out.println("In level one, you have three chances to correctly guess the number. The number will be anywhere from 1 to 10.");
- System.out.println("In level two, you have five chances to correctly guess the number. The number will be anywhere from 1 to 50.");
- System.out.println("In level three, you have seven chances to correctly guess the number. The number will be anywhere from 1 to 100.");
- System.out.println();
- System.out.println("***Press ENTER to Continue***");
- scan1.nextLine();
- System.out.println("To guess a number, type the number in and press enter.");
- System.out.println("You will be told whether or not you correctly guessed the number.");
- System.out.println("If you guess incorrectly, you will be given a hint as to whether the number is higher or lower than what you guessed.");
- System.out.println("If you run out of chances to correctly guess the number, then you lose.");
- System.out.println();
- System.out.println("***Press ENTER to Start Game***");
- scan1.nextLine();
- System.out.println("Please begin by selecting a level:");
- System.out.println();
- System.out.println("Level One - numbers 1-10 (type \"1\" to play)");
- System.out.println("Level Two - numbers 1-50 (type \"2\" to play)");
- System.out.println("Level Three - numbers 1-100 (type \"3\" to play)");
- System.out.println();
- System.out.println("Type \"exit\" to end the game.");
- //Allows the program to continue running if the user selects an invalid number or string during the level selection.
- while (true)
- {
- doMethod();
- }
- }
- public static void doMethod()
- {
- //Creates a second Scanner to allow the user to select the level they wish to play
- Scanner scan2 = new Scanner(System.in);
- String level = scan2.nextLine();
- //Level One
- {
- if (level.equals("1"))
- {
- System.out.println("You chose Level 1. You have 3 chances to guess the number.");
- int num = LevelOne.randInt();
- int guess;
- int chances=3;
- boolean win = false;
- while (win == false)
- {
- if (chances == 0)
- {
- System.out.println();
- System.out.println("You have no chances left! You lose!");
- System.out.println("The number was " + num + ".");
- System.out.println();
- System.out.println("Thanks for playing!");
- System.exit(0);
- }
- else
- {
- System.out.println();
- System.out.println("Guess a number from 1 to 10.");
- guess = scan2.nextInt();
- chances--;
- if (guess < num)
- {
- System.out.println("Your guess was too low.");
- System.out.println();
- System.out.println("Attempts remaining: " + chances);
- }
- else if (guess > num)
- {
- System.out.println("Your guess was too high.");
- System.out.println();
- System.out.println("Attempts remaining: " + chances);
- }
- else if (guess == num)
- {
- win = true;
- }
- }
- }
- System.out.println("You win! Congratulations!");
- System.out.println("The number was " + num + ".");
- System.out.println();
- System.out.println("Thanks for playing!");
- System.exit(0);
- }
- //Level Two
- else if (level.equals("2"))
- {
- System.out.println("You chose Level 2. You have 5 chances to guess the number.");
- int num = LevelTwo.randInt();
- int guess;
- int chances=5;
- boolean win = false;
- while (win == false)
- {
- if (chances == 0)
- {
- System.out.println();
- System.out.println("You have no chances left! You lose!");
- System.out.println("The number was " + num + ".");
- System.out.println();
- System.out.println("Thanks for playing!");
- System.exit(0);
- }
- else
- {
- System.out.println();
- System.out.println("Guess a number from 1 to 50.");
- guess = scan2.nextInt();
- chances--;
- if (guess < num)
- {
- System.out.println("Your guess was too low.");
- System.out.println();
- System.out.println("Attempts remaining: " + chances);
- }
- else if (guess > num)
- {
- System.out.println("Your guess was too high.");
- System.out.println();
- System.out.println("Attempts remaining: " + chances);
- }
- else if (guess == num)
- {
- win = true;
- }
- }
- }
- System.out.println("You win! Congratulations!");
- System.out.println("The number was " + num + ".");
- System.out.println();
- System.out.println("Thanks for playing!");
- System.exit(0);
- }
- //Level Three
- else if (level.equals("3"))
- {
- System.out.println("You chose Level 3. You have 7 chances to guess the number.");
- int num = LevelTwo.randInt();
- int guess;
- int chances=7;
- boolean win = false;
- while (win == false)
- {
- if (chances == 0)
- {
- System.out.println();
- System.out.println("You have no chances left! You lose!");
- System.out.println("The number was " + num + ".");
- System.out.println();
- System.out.println("Thanks for playing!");
- System.exit(0);
- }
- else
- {
- System.out.println();
- System.out.println("Guess a number from 1 to 100.");
- guess = scan2.nextInt();
- chances--;
- if (guess < num)
- {
- System.out.println("Your guess was too low.");
- System.out.println();
- System.out.println("Attempts remaining: " + chances);
- }
- else if (guess > num)
- {
- System.out.println("Your guess was too high.");
- System.out.println();
- System.out.println("Attempts remaining: " + chances);
- }
- else if (guess == num)
- {
- win = true;
- }
- }
- }
- System.out.println("You win! Congratulations!");
- System.out.println("The number was " + num + ".");
- System.out.println();
- System.out.println("Thanks for playing!");
- System.exit(0);
- }
- //Allows the user to exit the program if they wish
- else if (level.equals("exit"))
- {
- System.exit(0);
- }
- //Allows the user to enter another input if their first one was invalid
- else
- System.out.println("Please select 1, 2, or 3, or type \"exit\" to end the game.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement