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? - import java.util.*;
-
-
public class GuessingGame
-
{
-
int maxNumber;
-
/**
-
* Constructor setting the input number
-
* and the max number
-
* @param int num
-
*/
-
public GuessingGame(int num)
-
{
-
String number = "";
-
maxNumber = num;
-
}
-
-
/**
-
* Random number between 1 and max is generated for the
-
* user to guess. User is asked to guess a number. If number
-
* guessed was correct then return the number of guesses.
-
*/
-
public void Play()
-
{
-
Random ranNum = new Random();
-
int correctNum = ranNum.nextInt(maxNumber) + 1; //the correct number guessed equals the max random number
-
Scanner in = new Scanner(System.in);
-
System.out.println("Guess a number");
-
int number = in.nextInt();
-
-
-
if(number == correctNum) //If number correct, return number of guesses
-
{
-
System.out.println(in.nextInt() + " good guess");
-
-
}
-
/**
-
* if number guessed is null or user quits then
-
* return message stating "thanks for playing" and -1
-
* @return
-
*/
-
else if (number == -1)
-
{
-
System.out.println("Thanks for trying" + -1);
-
}
-
/**
-
* if number guessed is too high,
-
* then return message stating number is too high
-
* and to try again
-
* @return
-
*/
-
else if (number > correctNum)
-
{
-
System.out.println("The number is too high, try again");
-
}
-
/**
-
* if number guessed too low, then
-
* return message stating number is too low
-
* and to try again
-
* @return
-
*/
-
else if (number < correctNum)
-
{
-
System.out.println("The number is too low, please try again");
-
}
-
-
-
}
-
}
6 2468
When you use in.nextInt(), it jumps one number further each time, so if you do - if(in.nextInt() == 1){System.out.print(1);}
-
if(in.nextInt() == 2){System.out.print(2);}
-
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: - int nextInt = in.nextInt();
-
if(nextInt == 1){System.out.print(1);}
-
if(nextInt == 2){System.out.print(2);}
-
if(nextInt == 2){System.out.print(3);}
Greetings,
Nepomuk
I am sorry, but I am not understanding what you are trying to say.
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 - if(in.nextInt() == 3){System.out.println(3);}
-
if(in.nextInt() > 2){System.out.println("Bigger");}
-
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: - int nextInt = in.nextInt();
and work with that instead of reading the next number in line again and again and again.
Greetings,
Nepomuk
I would just add
int number = in.nextInt();
after you state that the guess was too low or too high. So for example: - else if (number > correctNum)
-
{
-
System.out.println("The number is too high, try again");
-
int number = in.nextInt();
-
}
and - else if (number < correctNum)
-
{
-
System.out.println("The number is too low, please try again");
-
int number = in.nextInt();
-
}
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
Ok I understand what you are saying, but what do you mean by "make sure you save it."
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. - import java.util.*;
-
-
public class GuessingGame
-
{
-
int maxNumber;
-
/**
-
* Constructor setting the input number
-
* and the max number
-
* @param int num
-
*/
-
public GuessingGame(int num)
-
{
-
maxNumber = num;
-
}
-
-
/**
-
* Random number between 1 and max is generated for the
-
* user to guess. User is asked to guess a number. If number
-
* guessed was correct then return the number of guesses.
-
*/
-
public int Play()
-
{
-
Random ranNum = new Random();
-
int correctNum = ranNum.nextInt(maxNumber) + 1; //the correct number guessed equals the max random number
-
int number = 0;
-
int numGuess = 0;
-
-
while( number != correctNum)
-
{
-
-
Scanner in = new Scanner(System.in);
-
-
System.out.println("Guess a number");
-
number = in.nextInt();
-
-
if(number == correctNum) //If number correct, return number of guesses
-
{
-
System.out.println("Good guess");
-
++ numGuess;
-
return numGuess;
-
-
}
-
/**
-
* if number guessed is null or user quits then
-
* return message stating "thanks for playing" and -1
-
* @return
-
*/
-
else if (number == -1)
-
{
-
System.out.println("Thanks for trying ");
-
return -1;
-
}
-
/**
-
* if number guessed is too high,
-
* then return message stating number is too high
-
* and to try again
-
* @return
-
*/
-
else if (number > correctNum)
-
{
-
System.out.println("The number is too high, try again");
-
++ numGuess;
-
-
}
-
/**
-
* if number guessed too low, then
-
* return message stating number is too low
-
* and to try again
-
* @return
-
*/
-
else if (number < correctNum)
-
{
-
System.out.println("The number is too low, please try again");
-
++ numGuess;
-
}
-
-
-
}
-
return -1; // should never get here
-
}
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| | |