Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help create HiLo game on Java
New Posts  All Forums:Forum Nav:

Help create HiLo game on Java

post #1 of 10
Thread Starter 
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:

/* 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.");
        }
    }


Main
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 3570K ASRock Z77 Pro3 Galaxy 560Ti G.Skills DDR3 
Hard DriveOSMonitorPower
WD Cavier 250GB + SimpleTech 320GB EXHDD Windows 7 x64 Samsung 19" SyncMaster 940BW OCZ 600w GameXStream 
Case
COOLER MASTER Centurion 5 
  hide details  
Reply
Main
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 3570K ASRock Z77 Pro3 Galaxy 560Ti G.Skills DDR3 
Hard DriveOSMonitorPower
WD Cavier 250GB + SimpleTech 320GB EXHDD Windows 7 x64 Samsung 19" SyncMaster 940BW OCZ 600w GameXStream 
Case
COOLER MASTER Centurion 5 
  hide details  
Reply
post #2 of 10
you need else if's rather than if's

for example

Code:
if(some case){
do something
}

else if(another case){
something else
}

else{
do the last case action
}
post #3 of 10
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.
Summer - '08
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Duo E7200 @3.8GHz Gigabyte GA-EP35-DS3L ASUS 5770 V-Tweak Ed. 1020/1380 @ 1.225V 4gb G.Skill ram 
Hard DriveOSMonitorKeyboard
160/320GB + 1TB WD Black Windows 7 Ultimate x64 Samsung Syncmaster 2493HM Saitek Eclipse II 
PowerCaseMouseMouse Pad
OCZ StealthXStream 600W Antec 900 Logitech G500 QcK+ 
  hide details  
Reply
Summer - '08
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Duo E7200 @3.8GHz Gigabyte GA-EP35-DS3L ASUS 5770 V-Tweak Ed. 1020/1380 @ 1.225V 4gb G.Skill ram 
Hard DriveOSMonitorKeyboard
160/320GB + 1TB WD Black Windows 7 Ultimate x64 Samsung Syncmaster 2493HM Saitek Eclipse II 
PowerCaseMouseMouse Pad
OCZ StealthXStream 600W Antec 900 Logitech G500 QcK+ 
  hide details  
Reply
post #4 of 10
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.
post #5 of 10
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
Fire Computer
(17 items)
 
Fire Laptop
(14 items)
 
Fire Server
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i5-3570K Ivy Bridge 3.4GHz (3.8GHz T... GIGABYTE GA-Z77X-UD5H LGA 1155 Intel Z77 HDMI S... GTX680-DC2O-2GD5 CORSAIR DOMINATOR GT 4GB 240-Pin DDR3 SDRAM DDR... 
Hard DriveHard DriveOptical DriveCooling
Seagate Samsung Light Scribe DVD Burner H50 
OSMonitorKeyboardPower
Windows 7 Professional 64 bit ASUS VH226H 21.5" Logitech G15 Corsair 850W 
CaseMouseAudioAudio
Corsair 500R Logitech M500  HDA X-Plosion 7.1 Boston Acustics BA7500 
CPUMotherboardGraphicsRAM
AMD Turion 64 X2 Mobile TL-64 30E5 NVIDIA GeForce Go 6150  Hyundai Electronics  
RAMHard DriveOS
Hyundai Electronics  250 GB W7 
  hide details  
Reply
Fire Computer
(17 items)
 
Fire Laptop
(14 items)
 
Fire Server
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i5-3570K Ivy Bridge 3.4GHz (3.8GHz T... GIGABYTE GA-Z77X-UD5H LGA 1155 Intel Z77 HDMI S... GTX680-DC2O-2GD5 CORSAIR DOMINATOR GT 4GB 240-Pin DDR3 SDRAM DDR... 
Hard DriveHard DriveOptical DriveCooling
Seagate Samsung Light Scribe DVD Burner H50 
OSMonitorKeyboardPower
Windows 7 Professional 64 bit ASUS VH226H 21.5" Logitech G15 Corsair 850W 
CaseMouseAudioAudio
Corsair 500R Logitech M500  HDA X-Plosion 7.1 Boston Acustics BA7500 
CPUMotherboardGraphicsRAM
AMD Turion 64 X2 Mobile TL-64 30E5 NVIDIA GeForce Go 6150  Hyundai Electronics  
RAMHard DriveOS
Hyundai Electronics  250 GB W7 
  hide details  
Reply
post #6 of 10
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.
post #7 of 10
Thread Starter 
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.
Edited by TaiDinh - 11/17/08 at 4:28pm
Main
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 3570K ASRock Z77 Pro3 Galaxy 560Ti G.Skills DDR3 
Hard DriveOSMonitorPower
WD Cavier 250GB + SimpleTech 320GB EXHDD Windows 7 x64 Samsung 19" SyncMaster 940BW OCZ 600w GameXStream 
Case
COOLER MASTER Centurion 5 
  hide details  
Reply
Main
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 3570K ASRock Z77 Pro3 Galaxy 560Ti G.Skills DDR3 
Hard DriveOSMonitorPower
WD Cavier 250GB + SimpleTech 320GB EXHDD Windows 7 x64 Samsung 19" SyncMaster 940BW OCZ 600w GameXStream 
Case
COOLER MASTER Centurion 5 
  hide details  
Reply
post #8 of 10
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:
/* 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:
/* 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!");
}
    }
}

Edited by BiG O - 11/17/08 at 5:03pm
post #9 of 10
Thread Starter 
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!
Main
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 3570K ASRock Z77 Pro3 Galaxy 560Ti G.Skills DDR3 
Hard DriveOSMonitorPower
WD Cavier 250GB + SimpleTech 320GB EXHDD Windows 7 x64 Samsung 19" SyncMaster 940BW OCZ 600w GameXStream 
Case
COOLER MASTER Centurion 5 
  hide details  
Reply
Main
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 3570K ASRock Z77 Pro3 Galaxy 560Ti G.Skills DDR3 
Hard DriveOSMonitorPower
WD Cavier 250GB + SimpleTech 320GB EXHDD Windows 7 x64 Samsung 19" SyncMaster 940BW OCZ 600w GameXStream 
Case
COOLER MASTER Centurion 5 
  hide details  
Reply
post #10 of 10
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++;

}
}
}
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help create HiLo game on Java