Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Number Guessing Game

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Number Guessing Game

    Hey, I'm brand new to Java and I just got an assignment in which I have to create a number guessing game. When you guess wrong, it is supposed to tell you the number is lower or higher depending on how you guess. when you get it right, it is supposed to tell you that you are correct and give you the amount of guesses you have taken. I was able to get up to here done. And hereafter is where I get really confused as to how to go about writing it.

    After this, I am supposed to prompt the player by asking if they would like to play again (allowing the user to type a "yes" or "no" regardless of case), and they can keep playing. When they finally answer "no", my program is supposed to say how many games they played and the players average amount of guesses per game.

    I have been working and working on this problem for nearly 4 hours now, getting it wrong every time. I know I'm going to have to have a loop inside of a loop, but as of right now I am completely lost and am looking for a push in the right direction. Any help would be great.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Number Guessing Game

    This is my program so far:
    import java.util.Scanner;
    class GuessNumber
    {
       public static void main(String[] args)
       {
          Scanner kb = new Scanner(System.in);
          int number, guess, guessCount;
     
     
             System.out.println("Guess my number between 1 and 100");
             number = (int)(100.0*Math.random()) + 1;
             guessCount = 0;
     
             while (true)
             {
                System.out.println("Enter your guess");
                guess = kb.nextInt();
                guessCount++;
                if (guess == number)
                   break;
                if (guess < number)
                   System.out.println("Too low");
                else
                   System.out.println("Too high");
             }
     
             System.out.println("Right");
             System.out.println("You took " + guessCount + " guesses");
             System.out.println("Do you want to play again? Enter Y/N");

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Number Guessing Game

    while(true)

    is not valid.

    if you had a boolean called

    playAgain and set it to true to begin with

    the statement

    while(playAgain == true)

    would be valid.

    As for guess lower and guess higher, I did find that the Integer class has a compareTo() method that returns 0 if equal, something greater than 0 if greater and something less than 0 if less than.

    You could simply do this:
     
     boolean guessRight = false;
    int numberOfGuesses = 0;
    while (guessedRight == false)
    {
    int number2 =  (int)(100.0*Math.random()) + 1;
     
    Integer temp2 = number2;
    int number = kb.nextInt();
     
    Integer temp = number;
     
    if (temp.compareTo(temp2) ==0)
    {
    System.out.println("You guessed correctly!");
    System.out.println(numberOfGuesses);
    numberOfGuesses++;
    guessedRight = true;
    }
     
    else
    {
    if (temp.compareTo(temp2) < 0)
    {
    System.out.println("You guessed lower than number.");
    numberOfGuesses++;
    }
     
    else
    {
    System.out.println("You guessed higher than number.");
    numberOfGuesses++;
    }
     
    }
    }
    Last edited by javapenguin; February 16th, 2011 at 06:27 PM.

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Number Guessing Game

    Quote Originally Posted by javapenguin
    while(true)

    is not valid.
    Based upon what information, because stating the above is wrong. Did you try it? Why would you say it is invalid?

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Wink Re: Number Guessing Game

    Quote Originally Posted by copeg View Post
    Based upon what information, because stating the above is wrong. Did you try it? Why would you say it is invalid?
    I'll admit I haven't tried it.

    However, does

    while(true) sound valid to you?

    I have seen

    while(variable == true)

    and while (method())

    but never

    while (true).

    Ok, I did find that's valid (don't know why it is) but as I've no clue what the true refers to, it still makes no sense to me from what I've learned.

    I've put

    while(true)
    {

    }

    statement

    and it never got to statement


    I also put

    while(false)
    {
    }
    statement

    and once again it never got to statement.

    Even if the syntax is valid, I don't see any way out other than a break or a System.exit(0);

    I suppose if you wanted to be technical, you could:

    while(true)
    {
     
    if (someCondtionIsMet)
    break;
    }

    Last edited by javapenguin; February 16th, 2011 at 07:33 PM.

  6. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Number Guessing Game

    However, does

    while(true) sound valid to you?
    Yes, it does. Anything is 'valid' if it compiles and behaves the way you want it to.

    Even if the syntax is valid, I don't see any way out other than a break or a System.exit(0);
    And returning from the method, and throwing an Exception (although this would be bad practice).

Similar Threads

  1. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 11:41 AM
  2. Guessing Game begginner question
    By okupa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 07:31 AM
  3. im in a hurry!!Help with a programm..java game of guessing a word
    By mr_doctor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:17 AM
  4. begginer wondering why his guessing game won't work
    By Ligawulf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 11:22 PM
  5. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM