Overclock.net banner

1 - 10 of 10 Posts

·
Premium Member
Joined
·
10,440 Posts
Discussion Starter #1
I don't see what I am doing wrong. After I inputted the number 50 as my guess, I am suppose to get a message telling me if I am too high, too low or I am correct, but it's not.

If I enter the number 0 to quit, it does the same thing. I'm not even sure how to declare the 0 to be the number that quits the game.

Any ideas?

My error:



Instructions:

Quote:


Design and implement an application that plays the Hi-Low guessing game with numbers. The program should pick a random number between 1 and 100 (inclusive), then keep asking the user to guess the number. ON each guess, report to the user that he or she is correct or that the guess is high or low. Keep accepting guesses until the user guesses correctly or quits. Use a sentinel value to determine wheater the user wants to quit. Count the number of guesses and report that value when the user guesses correctly. At the end of each game (by quitting or a correct guess), ask whether the user wants to play again. Keep playing games until the user chooses to stop.

My code:

Code:
Code:
[CODE]
/* Anthony Dinh
Exercise 3.11
November 14, 2008
*/

import java.util.Random;

public class example
{
    public static void main (String[] args)
    {
        final int MAX = 100;
        int answer, guess;

        EasyReader Keyboard = new EasyReader();
        System.out.print ("I'm thinking of a number between 1 and "
                            + MAX + ". Guess what it is: (or enter 0 to quit) ");
        guess = Keyboard.readInt();

        Random generator = new Random(); //Random generator. 1 to 100.
        answer = generator.nextInt(MAX) +1;

        if (guess == answer){ //If guess equals answer
            System.out.println ("You got it! Good guessing!");

            }if (guess == 0){ //Game ends
            //System.out.println ("You have ended your game. Goodbye.");

        }while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.

            }if (guess > answer && guess != 0){ //If guess is higher than answer
        System.out.println ("You guessed too high!");
        guess = Keyboard.readInt();

            }else{
                    if (guess < answer && guess != 0) //If guess is lower than answer
                    System.out.println ("You guessed too low!");
                    guess = Keyboard.readInt();

                        }if (guess == answer){ //If guess equals answer
                            System.out.println ("You got it! Good guessing!");

                            }if (guess == 0){ //Game ends
                                System.out.println ("You have ended your game. Goodbye.");
        }
    }
}
[/CODE]

 

·
Registered
Joined
·
872 Posts
I'd use a do while loop, so say,
do {
// code
} while (guess != 0)
At the end of the loop (after), just say the game ended.
Now on the inside, you want the following conditions

if (guess > answer) {
System.out.println("You guessed too high, try a lower number!");
// Take input again.
}

else if (guess < answer) {
System.out.println("You guessed to low, try a higher number!");
(Take input again).
}

else if (guess == answer) {
// System.out.println("You guessed it right!");
break;
}

In your code, there's nothing in your while loop, so it won't run more than once lol.
 

·
Premium Member
Joined
·
4,343 Posts
Quote:

Originally Posted by xShishy View Post
I'd use a do while loop, so say,
do {
// code
} while (guess != 0)
At the end of the loop (after), just say the game ended.
Now on the inside, you want the following conditions

if (guess > answer) {
System.out.println("You guessed too high, try a lower number!");
// Take input again.
}

else if (guess < answer) {
System.out.println("You guessed to low, try a higher number!");
(Take input again).
}

else if (guess == answer) {
// System.out.println("You guessed it right!");
break;
}

In your code, there's nothing in your while loop, so it won't run more than once lol.
A loop would be a better way of doing it. Just need else if's if you're going to do it your way. The only problem with your way is that you can reach the end, and still not have a solution, which can be fixed by doing a loop.
 

·
Premium Member
Joined
·
4,343 Posts
Quote:

Originally Posted by FireMarshallBill View Post
One thing that I noticed was that your while loop only looks like it contains

"//If guess and 0 is not answer, continue."

The while loop should contain the if statements if guess==answer if guess>anwwser ...etc
he handles the other cases once inside the while loop.
 

·
Premium Member
Joined
·
10,440 Posts
Discussion Starter #7
I want to thank you guys for helping me, but I don't have a single idea on what to do.

@xShishy

I've tried your way and I got an infinite loop.

@BiG O

I'm not sure how to use the else-if statement in my code.

EDIT:

Quote:

Originally Posted by FireMarshallBill View Post
One thing that I noticed was that your while loop only looks like it contains

"//If guess and 0 is not answer, continue."

The while loop should contain the if statements if guess==answer if guess>anwwser ...etc
I didn't see your post. Let me think now.
 

·
Premium Member
Joined
·
4,343 Posts
Here is your code, with else if's in there. I can't compile it, because I'm at work, and don't have the EasyReader class here, nor do I know why you're using it.

Code:
Code:
/* Anthony Dinh
Exercise 3.11
November 14, 2008
*/

import java.util.Random;

public class example
{
    public static void main (String[] args)
    {
        final int MAX = 100;
        int answer, guess;

        EasyReader Keyboard = new EasyReader();
        System.out.print ("I'm thinking of a number between 1 and "
                            + MAX + ". Guess what it is: (or enter 0 to quit) ");
        guess = Keyboard.readInt();

        Random generator = new Random(); //Random generator. 1 to 100.
        answer = generator.nextInt(MAX) +1;

        if (guess == answer){ //If guess equals answer
            System.out.println ("You got it! Good guessing!");

        }
        else if (guess == 0){ //Game ends
            //System.out.println ("You have ended your game. Goodbye.");

        }
        while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.

        if (guess > answer && guess != 0){ //If guess is higher than answer
        System.out.println ("You guessed too high!");
        guess = Keyboard.readInt();

            }
            else{
                if (guess < answer && guess != 0){ //If guess is lower than answer
                    System.out.println ("You guessed too low!");
                    guess = Keyboard.readInt();
}

                else if (guess == answer){ //If guess equals answer
                    System.out.println ("You got it! Good guessing!");
}
                else if (guess == 0){ //Game ends
                                System.out.println ("You have ended your game. Goodbye.");
}
}
        }
        if ( guess == answer){
System.out.println ("You got it! Good guessing!");
}
    }
}
I fixed your brackets and tabbing too because it was driving me insane
. Looks like pasting it in here didnt get all my tabbing the way I'd like it, but you get the idea. You can see the difference with the else if's. Tell me how that works out.

EDIT: I used scanner rather than your easy reader just to test, and it works perfectly. I also just added another line outside the while loop at the end, so it prints out that you won. It didn't do this before. You should be set now.

Here it is using scanner rather than using EasyReader, just for reference. It's not very different at all.

Code:
Code:
/* Anthony Dinh
Exercise 3.11
November 14, 2008
*/

import java.util.Random;
import java.util.Scanner;

public class example
{
    public static void main (String[] args)
    {
        final int MAX = 100;
        int answer, guess;

        Scanner Keyboard = new Scanner(System.in);
        System.out.print ("I'm thinking of a number between 1 and "
                            + MAX + ". Guess what it is: (or enter 0 to quit) ");
        guess = Keyboard.nextInt();
//guess = 12;
        Random generator = new Random(); //Random generator. 1 to 100.
        answer = generator.nextInt(MAX) +1;

        if (guess == answer){ //If guess equals answer
            System.out.println ("You got it! Good guessing!");

        }
        else if (guess == 0){ //Game ends
            System.out.println ("You have ended your game. Goodbye.");

        }
        while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.

        if (guess > answer && guess != 0){ //If guess is higher than answer
        System.out.println ("You guessed too high!");
        guess = Keyboard.nextInt();

            }
            else{
                if (guess < answer && guess != 0){ //If guess is lower than answer
                    System.out.println ("You guessed too low!");
                    guess = Keyboard.nextInt();
}

                else if (guess == answer){ //If guess equals answer
                    System.out.println ("You got it! Good guessing!");
}
                else if (guess == 0){ //Game ends
                                System.out.println ("You have ended your game. Goodbye.");
}
}
        }
        if ( guess == answer){
System.out.println ("You got it! Good guessing!");
}
    }
}
 

·
Premium Member
Joined
·
10,440 Posts
Discussion Starter #9
Thanks BiG O! Yeah, we're using EasyReader. That's all I can say, lol. This is my first year of programming.

Your code compiled successfully and functions as it is suppose to. I'm going to read over your code a few times and then rewrite the entire program again without using your code as a source.

Thanks again to you and everyone who helped!
 

·
Registered
Joined
·
1 Posts
import java.util.Random;
import java.util.Scanner;

public class Hilo {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
Random generator = new Random();
int guessNumber;
int numberOfGuesses = 0;
int numberToGuess = generator.nextInt(100) + 1;

System.out.println("Welcome to the HILO guessing game. Please enter a number from 1 to 100");
while (numberOfGuesses < 7) {

System.out.print("Guess " + (numberOfGuesses + 1) + ": ");
guessNumber = in.nextInt();
if (guessNumber < numberToGuess)
System.out.println("Your guess is lower.");
else if (guessNumber > numberToGuess)
System.out.println("Your guess is higher.");
else {
System.out.println("You guessed it right.");
break;
}
numberOfGuesses++;

}
}
}
 
1 - 10 of 10 Posts
Top