Question:
guessing game java programming?
Jay-R R
2012-03-17 19:09:55 UTC
I'm having trouble writing a java program where:

Guessing Game: Computer generates a number in a range between 1 and 100. And then ask user enter a guess. When user’s guess is lower than the answer, then give a hint (Too low) and then show a new range for next guess as shown in the sample run. If player enters a value outside of valid range, then you should give a warning and request a valid input. Player can guess until they get the right answer and program tracks the number of guess.
Hint: Use the Math.random() method to calculate a random value.

Here is a sample run:

Okay, I decided a number between 1 and 100. Try a guess[1,100]: 45
Noop! Try lower. [1,44]: 20
Noop! Try lower. [1,19]: 10
Noop! Try higher. [11,19]: 15
YES!! You got it after 4 times.

I can write the simpler code of what it's asking but I'm having trouble when it asks "When user’s guess is lower than the answer, then give a hint (Too low) and then show a new range for next guess as shown in the sample run."

Here's the code I have:
int secretNumber=(int)(101 * Math.random ()) + 1 ;

int guess;
int numberOfGuesses;

Scanner input = new Scanner(System.in);
numberOfGuesses = 0;

System.out.println("Guess a number between 1 and 100:");

do {

guess = input.nextInt();
numberOfGuesses++;

if(guess > secretNumber){
System.out.println("Too high.");}

else if(guess < secretNumber){
System.out.println("Too low.");}

else{
System.out.println("Good job.");
System.out.println("Number of Guesses: "+ numberOfGuesses);}}
while(guess != secretNumber);
}}
Six answers:
James Bond
2012-03-17 19:53:18 UTC
int secretNumber=(int)(101 * Math.random ()) + 1 ;

int Low=1, High=100;



int guess;

int numberOfGuesses;



Scanner input = new Scanner(System.in);

numberOfGuesses = 0;



System.out.println("Guess a number between 1 and 100:");



do {



guess = input.nextInt();

numberOfGuesses++;



if(guess > secretNumber){

System.out.println("Too high.");

High=guess-1;

System.out.println("Noop. Try ["+Low+"-"+High+"]");



}



else if(guess < secretNumber){

System.out.println("Too low.");

Low=guess+1;

System.out.println("Noop. Try ["+Low+"-"+High+"]");



}



else{

System.out.println("Good job.");

System.out.println("Number of Guesses: "+ numberOfGuesses);}}

while(guess != secretNumber);

}}
jedediah
2016-12-30 08:28:07 UTC
Guessing Game Java Code
depaola
2016-11-10 09:03:51 UTC
Java Guessing Game
Zach
2012-03-17 19:17:47 UTC
I can't really tell what your asking.

But if its with the "To Low"

Instead of it being an else if statement make it just an if statement. Delete the else.That should work.

*EDIT*

Actually your whole programming for this is bad. I don't even know java. But I don't know how this is even runnign :0

here let me just recode this for you.

do {

guess = input.nextInt();

numberOfGuesses++;



if(guess > secretNumber){

System.out.println("Too high.");}



if(guess < secretNumber){

System.out.println("Too low.");}



while(guess!= secretNumber);

}





System.out.println("Good job.");

System.out.println("Number of Guesses: "+ numberOfGuesses);





And that will work a lot better. At least it should... You don't want to make the "success text" inside the loop that determines whether or not they will win.

Hopefully that works
anonymous
2016-05-17 05:11:23 UTC
Hope this works, i don't have a java compiler installed to check for errors. while(guess!=number) { System.out.println("Guess a number between 1 and 100:"); guess = Keyboard.nextInt(); if(guess>number) System.out.println("Lower"); if(guess
anonymous
2012-03-17 19:21:08 UTC
http://www.java-made-easy.com/guessing-game.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...