473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Guessing game almost done, but not working correctly

Hi, I am making a guessing game from the java language. The user is asked to input a number. The number is 1 through a random max number. If the number guessed is correct, then the program should return the number of guesses. If the number guessed is null (the player clicked Cancel), the user should get a “Thanks for trying” message, and should return -1. If the number is too high, the player should get a message telling them that the number is too high and should get to try again. If the number is too low, the user should get a message telling them that the number is too low, and they should get to try again.

The problem is when I input a number my compiler will read it correctly. It will notify me that I entered a number too low or too high, but it wont let me try again unless I start the whole program over. I feel that I need to make some sort of loop to get this done, but how?



Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class GuessingGame
  4. {   
  5.     int maxNumber;    
  6. /**
  7.  * Constructor setting the input number
  8.  * and the max number
  9.  * @param int num
  10.  */
  11.      public GuessingGame(int num)
  12.      {
  13.        String number = "";
  14.        maxNumber = num;
  15.      }
  16.  
  17. /**
  18. * Random number between 1 and max is generated for the 
  19. * user to guess. User is asked to guess a number. If number
  20. * guessed was correct then return the number of guesses.
  21. */
  22.         public void Play()
  23.         {
  24.           Random ranNum = new Random(); 
  25.           int correctNum = ranNum.nextInt(maxNumber) + 1; //the correct number guessed equals the max random number
  26.           Scanner in = new Scanner(System.in);  
  27.           System.out.println("Guess a number");
  28.            int number = in.nextInt();
  29.  
  30.  
  31.           if(number == correctNum) //If number correct, return number of guesses
  32.           {
  33.             System.out.println(in.nextInt() + " good guess");
  34.  
  35.           }
  36. /**
  37. * if number guessed is null or user quits then 
  38.  * return message stating "thanks for playing" and -1
  39.  * @return
  40.  */
  41.           else if (number == -1) 
  42.           {
  43.             System.out.println("Thanks for trying" + -1);   
  44.           }
  45. /**
  46.  * if number guessed is too high,
  47.  * then return message stating number is too high
  48.  * and to try again
  49.  * @return
  50.  */
  51.           else if (number > correctNum) 
  52.           {
  53.               System.out.println("The number is too high, try again");
  54.           }
  55. /**
  56.  * if number guessed too low, then 
  57.  * return message stating number is too low
  58.  * and to try again
  59.  * @return 
  60.  */
  61.           else if (number < correctNum)
  62.           {
  63.              System.out.println("The number is too low, please try again");   
  64.           }
  65.  
  66.  
  67.         }
  68. }
Mar 17 '09 #1
6 2468
Nepomuk
3,112 Expert 2GB
When you use in.nextInt(), it jumps one number further each time, so if you do
Expand|Select|Wrap|Line Numbers
  1. if(in.nextInt() == 1){System.out.print(1);}
  2. if(in.nextInt() == 2){System.out.print(2);}
  3. if(in.nextInt() == 2){System.out.print(3);}
and just input a 3, it will be expecting two further inputs.

What you want to do is more like this:
Expand|Select|Wrap|Line Numbers
  1. int nextInt = in.nextInt();
  2. if(nextInt == 1){System.out.print(1);}
  3. if(nextInt == 2){System.out.print(2);}
  4. if(nextInt == 2){System.out.print(3);}
Greetings,
Nepomuk
Mar 17 '09 #2
I am sorry, but I am not understanding what you are trying to say.
Mar 17 '09 #3
Nepomuk
3,112 Expert 2GB
OK, imagine the input as a stream of numbers separated by white spaces, e.g. 3 2 6 21 5
Now, in.nextInt() will read the first number in the stream and remove it. So with
Expand|Select|Wrap|Line Numbers
  1. if(in.nextInt() == 3){System.out.println(3);}
  2. if(in.nextInt() > 2){System.out.println("Bigger");}
  3. else {System.out.println("Not bigger");}
it will print a '3' and "Not bigger" (because it's comparing the 2 now) and leave the stream 6 21 5
You may notice, that there's a further 3 in that stream. The program doesn't (and shouldn't) care at this moment.

Now, you want to check the same number a few times. So make sure you save it:
Expand|Select|Wrap|Line Numbers
  1. int nextInt = in.nextInt();
and work with that instead of reading the next number in line again and again and again.

Greetings,
Nepomuk
Mar 17 '09 #4
I would just add

int number = in.nextInt();

after you state that the guess was too low or too high. So for example:

Expand|Select|Wrap|Line Numbers
  1. else if (number > correctNum)  
  2.           { 
  3.               System.out.println("The number is too high, try again");
  4.               int number = in.nextInt();
  5.           } 
and

Expand|Select|Wrap|Line Numbers
  1. else if (number < correctNum) 
  2.           { 
  3.              System.out.println("The number is too low, please try again");
  4.              int number = in.nextInt();    
  5.           }
that way you are requiring the user or player to enter a new number (guess) each time they are either too high or too low.

I think that would work anyways...

Adam
Mar 17 '09 #5
Ok I understand what you are saying, but what do you mean by "make sure you save it."
Mar 18 '09 #6
Ok I believe I have it now. I did make a while loop and inside that while loop I also had the number of guesses incremented. So the program would also have output how many times the user tried to guess the correct number.


Expand|Select|Wrap|Line Numbers
  1. import java.util.*; 
  2.  
  3. public class GuessingGame 
  4. {    
  5.     int maxNumber;     
  6. /** 
  7.  * Constructor setting the input number 
  8.  * and the max number 
  9.  * @param int num 
  10.  */ 
  11.      public GuessingGame(int num) 
  12.      { 
  13.        maxNumber = num; 
  14.      } 
  15.  
  16. /** 
  17. * Random number between 1 and max is generated for the  
  18. * user to guess. User is asked to guess a number. If number 
  19. * guessed was correct then return the number of guesses. 
  20. */ 
  21.         public int Play() 
  22.         { 
  23.           Random ranNum = new Random();  
  24.           int correctNum = ranNum.nextInt(maxNumber) + 1; //the correct number guessed equals the max random number 
  25.           int number = 0;
  26.           int numGuess = 0;
  27.  
  28.           while( number != correctNum)
  29.           {
  30.  
  31.             Scanner in = new Scanner(System.in); 
  32.  
  33.             System.out.println("Guess a number"); 
  34.             number = in.nextInt(); 
  35.  
  36.             if(number == correctNum) //If number correct, return number of guesses 
  37.             { 
  38.             System.out.println("Good guess"); 
  39.             ++ numGuess;
  40.             return numGuess;
  41.  
  42.             } 
  43. /** 
  44. * if number guessed is null or user quits then  
  45.  * return message stating "thanks for playing" and -1 
  46.  * @return 
  47.  */ 
  48.             else if (number == -1)  
  49.             { 
  50.                 System.out.println("Thanks for trying ");  
  51.                 return -1;
  52.             } 
  53. /** 
  54.  * if number guessed is too high, 
  55.  * then return message stating number is too high 
  56.  * and to try again 
  57.  * @return 
  58.  */ 
  59.             else if (number > correctNum)  
  60.             { 
  61.                   System.out.println("The number is too high, try again"); 
  62.                   ++ numGuess;
  63.  
  64.             }     
  65. /** 
  66.  * if number guessed too low, then  
  67.  * return message stating number is too low 
  68.  * and to try again 
  69.  * @return  
  70.  */ 
  71.             else if (number < correctNum) 
  72.             { 
  73.                  System.out.println("The number is too low, please try again");    
  74.                  ++ numGuess;
  75.             } 
  76.  
  77.  
  78.         } 
  79.         return -1; // should never get here
  80.     }
Mar 20 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: BlackHawke | last post by:
Hello! This is my second post. Ppl really helped me with the first. I hope there are answers for this one as well I own a game company (www.aepoxgames.net) releasing the beta for our first...
4
by: Moosebumps | last post by:
When I have time, I am planning to evaluate Python for console game development (on Playstation 2, GameCube, and Xbox). Does anyone have any experience with this? Pretty much the only resource...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
2
by: Brian Basquille | last post by:
Hello all. Air Hockey game is due up in just over a month. And i've about 2 and a half weeks to work on it. So, i need some feedback and advice. ...
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
5
by: Kraken | last post by:
Hi, i have a bit of a problem here. I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file...
9
KoreyAusTex
by: KoreyAusTex | last post by:
I have written a program using method calls as opposed to nested while loops, or rather a while within a while, but for some reason the only way I can get the program to terminate is to type break in...
7
by: dseto200 | last post by:
I'm able to create a guessing number game, but I can limit the amount of guesses to less than 3. Can someone tell me what i'm doing wrong? # Guess My Number # The computer picks a random number ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.