Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java Guessing Game Help

    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;
                }
            }
        }
    }

  • #2
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have updated my code... I've got to the stage where the user can guess a number between 1 and 10 and the program output's weather the number is correct, higher or lower. However, the problem I have is programming the system to allow the user to enter the number of live/attempts the user is allowed to guess. Any advice/help would be sincerly apreciated.

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package GuessingGame;
    
    /**
     *
     * @author daspeysm
     */
    import java.util.Scanner;
    import java.util.Random;
    
    public class Main {
    
        public static void main(String[] args) {
    
            Random rand = new Random();
            int noGuess = rand.nextInt(10);
            int noTries = 0;
            Scanner input = new Scanner(System.in);
            int guess;
            boolean win = false;
    
            while (win == false) {
    
                System.out.println("Enter number between 1 and 10");
                guess = input.nextInt();
                noTries++;
    
                if (guess == noGuess) {
                    win = true;
    
                } else if (guess < noGuess) {
                    System.out.println("Higher! Try again");
                } else if (guess > noGuess) {
                    System.out.println("Lower! Try again");
                }
            }
    
            System.out.println("You won");
            System.out.println(noTries);
        }
    }

  • #3
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    You'll need to ask the user for the number of tries and store it in a variable
    You could then alter you while-condition to "win equals false OR noTries equals userEnteredNoTries

    If you go this route when your program exits the loop it will always print "You won". So you'll want to check if "win equals true" print "You won" else print "You Lost".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •