Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java number guessing game question.

Options
  • 24-01-2012 7:38pm
    #1
    Registered Users Posts: 130 ✭✭


    Hey guys I'm currently trying to write a program for a guessing game, basically the user has to guess a number between 1-99, if they get it right it'll ask them do they want to play again, if they get it wrong it will display a hint of them being too high/low. It also has to display each time the number of attempts it has taken to get it right, also at any time the user can exit by pressing x(this is the bit that's causing me the most amount of trouble) so far my coding is as follows:



    import java.util.Scanner;

    public class GuessingGame {

    public static void main(String[] args) {


    int genNum = (int) (Math.random() * 99) +1;
    int attempts = 0;
    Scanner scanner = new Scanner(System.in);
    int guess;
    char again = 'y';




    while (again=='y' || again=='Y') {
    while (genNum != guess && guess != 'x'){

    System.out.println("Welcome to the number guessing game \n please guess a number between 1 and 99 \n (x to exit)");
    guess = scanner.nextInt();
    attempts++;

    if (guess == genNum){
    }
    else if (guess > genNum){

    System.out.println("Your guess was too high, please try again"); }

    else if (guess < genNum){

    System.out.println("Your guess was too low, please guess again");}

    System.out.println("You've won");
    System.out.println("The numnber was " + genNum);
    System.out.println("It took you " + attempts + " tries");
    System.out.println("Would you like to play again? (y/n)");


    }
    }
    }
    }

    At the moment I'm getting errors saying I haven't declared 'guess', but I have?;s Any help with this would be greatly appreciated.


Comments

  • Registered Users Posts: 1,569 ✭✭✭Dante


    You might need to assign an initial value when declaring the variable, ie 'int guess = 0;'


  • Registered Users Posts: 20 lowlifer


    What environment are you using? I suggest getting Eclipse: http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/indigosr1

    If you hover the errors, it can suggest and apply fixes.

    Back to your problem: you have declared the variable "guess", but have not initialized it. In your second while, you check its value, but no values have been actually assigned to it.

    Secondly: your "guess" variable is an int, but in your second while you check if it's ever 'x'. If you try to assign a char value to an int variable, nothing good happens.

    Here is a "working" version:

    import java.util.Scanner;

    public class GuessingGame {

    public static void main(String[] args) {

    int genNum = (int) (Math.random() * 99) + 1;
    int attempts = 0;
    Scanner scanner = new Scanner(System.in);
    int guess = 'z';
    char again = 'y';

    while (again == 'y' || again == 'Y') {
    System.out.println("Welcome to the number guessing game \n please guess a number between 1 and 99 \n (x to exit)");
    while (genNum != guess && guess != 120) {
    guess = scanner.nextInt();
    attempts++;

    if (guess == genNum) {
    System.out.println("You've won");
    System.out.println("The number was " + genNum);
    System.out.println("It took you " + attempts + " tries");
    System.out.println("Would you like to play again? (y/n)");
    again = scanner.next().charAt(0);
    } else if (guess > genNum) {
    System.out.println("Your guess was too high, please try again");
    } else if (guess < genNum) {
    System.out.println("Your guess was too low, please guess again");
    }

    }
    }
    }
    }

    Try to think this through, logically. Write it on a piece of paper even if it feels lame. You want to be able to have more than one games, so you need a loop for that - first while. In each game, you will have one or more attempts - second loop - you chose while.

    The first while should run as long as the "again" variable is either y or Y. The "again" variable's value must be changed at the end of a game, depending on the user's reply (y/n). A user's prompted for a reply when he finishes a game.
    Also, when you start a game, you need to have a different value for your random number, whereas you're using the initial one.

    In your second while, there are 3 possibilities: the user chooses a lower number, higher number, or the correct number. When he chooses the correct number, that's the time to say congratulations you've won, what to do now.


  • Registered Users Posts: 130 ✭✭kev_like


    Thanks for your help and detailed response. I'm currently using Jcreator but I shall check out Eclipse tonight. I think I really have to think about my programs more before I jump into the deep end and just start coding, I think I'm going to do it all my programs logically on paper before I attempt them from now on :)
    There seems to be only one flaw with your fixed version of my code though, once I select to play another game (using Y or y) it sends it into an infinite loop of the message; "Welcome to the number guessing game \n please guess a number between 1 and 99 \n (x to exit)", what would you think could be causing this?
    Greatly appreciate the help :)


  • Registered Users Posts: 20 lowlifer


    This happens because genNum equals guess at the end of a game (after the user guessed it). To solve this, you need to make sure genNum has a different value at the start of each game.
    Declare it at the top like this:
    int genNum; - don't assign anything to it yet.
    
    In the first while (start of a game), give it a random value:
    genNum = (int) (Math.random() * 99) + 1;
    
    It might still get into an infinite loop if the new genNum is the same as before.


  • Registered Users Posts: 130 ✭✭kev_like


    lowlifer wrote: »
    This happens because genNum equals guess at the end of a game (after the user guessed it). To solve this, you need to make sure genNum has a different value at the start of each game.
    Declare it at the top like this:
    int genNum; - don't assign anything to it yet.
    
    In the first while (start of a game), give it a random value:
    genNum = (int) (Math.random() * 99) + 1;
    
    It might still get into an infinite loop if the new genNum is the same as before.


    That fixed it perfectly, thanks very much for all this, I've learned a lot from it :)


  • Advertisement
  • Registered Users Posts: 3,735 ✭✭✭Stuxnet


    bluej is a bit of of a stepping stone before jumping in to eclipse, I'd recommend that ide ! great for pointing out your errors, & very n00b friendly


  • Registered Users Posts: 8 paddyjoe1993


    Hi there.
    Just a small question for anyone that can help me out.
    I was wondering is there any book on java programing out there that i could buy to help me with this programing as i just cant get the hang of it.


Advertisement