Question:
Can someone help me with this guess my number game in java please?
trustcompany3000
2008-10-05 16:05:59 UTC
I need some help writing this guess my number game in java. Currently i am using Jcreator to do this. this is what i have to do.

Ask a user to think of a number between 1 and 100. Then have your program guess the user's number, and ask the user if the guess is correct, too high, or too low. Keep guessing until you guess correctly, and then display how many guesses it took you.

Your program needs to remember the range of values it's guessing within. At first, it needs to remember that 1 is the low number and 100 is the high number. The guess should always be halfway in between the low and the high, so the first guess should be 50. If 50 is too high, then you should change the high value to 49, because we know that we don't have to worry about guessing from 50-100 anymore. If 50 was too low, then change the low value to 51. Then guess halfway again.

If your program works correctly, it will never take you more than 7 guesses!

here is the code i have some far but it doesn't work the way i want it to. any advice will be appreicated.

import java.util.*;

public class L8_GuessNumber {

public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
Random rand = new Random();
int randNumber;
int tires;
int guess;
randNumber = rand.nextInt(100) + 1;


System.out.println("Welcome to Guess My Number!");
System.out.println();

System.out.println("Please enter a guess");
guess = reader.nextInt();
System.out.println();

do
{
if (guess > randNumber);
System.out.println("Too high!");

if (guess < randNumber);
System.out.println("Too low!");

} while (guess == randNumber);
System.out.println("Thats it, you got it!");
}

}
Three answers:
2008-10-05 19:01:21 UTC
If you read the problem carefully, you'll discover you're solving the wrong problem!



The question asks the user to pick a number and the computer to guess. Your solution has the computer picking a number and the user trying to guess it.



Still, let me help you with the errors in your code, because they will cause you problems no matter what algorithm you're trying to solve.



You cannot follow an if statement with a semicolon, because doing so ends the condition. For example:



//You can't put a semicolon after an if statement!

if (guess > randNumber);

//The following line will ALWAYS exectute:

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



//Try this instead:

if (guess > randNumber){

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

} // end if



Use braces ({}) to surround the lines which should execute when the condition is true.



The other major problem is your looping condition. Your main code looks something like this:



do {



//program code



} while (guess == randNumber);



You don't really want the code to repeat as long as guess is equal to the random number. You want to go while guess is NOT equal to the random number, because that means the user isn't correct yet, and needs to guess again...



However, the bigger problem remains; you're solving the wrong problem.



Have the user generate the random number, not the computer. Let the computer generate a guess, and give that to the user. Let the user indicate too high, too low, or correct.

Use the algorithm supplied by your teacher to determine what number the computer guesses each time
2008-10-05 16:12:11 UTC
In the loop you might want to include accepting input from the user and instead of:

while(guess == randNumber);



use:

while(guess != randNumber);



Make sure the user knows what range the number is between otherwise it might get confusing
reenu
2016-12-15 08:49:20 UTC
i assume you may desire to apply a mutually as loop, yet i think of considering you comprehend the max style of tries the person could have, a for loop might extra proper suffice. some thing like: Random ranNumGen = new Random(); int numToGuess = ranNumGen.nextInt(10) + a million; device.out.println(numToGuess); int guess = 0; device.out.println("i'm questioning of a variety a million to 10. you should guess what this is in 3 tries"); for (int i=a million; i<=3; i++) { device.out.println("enter your guess: "); guess = kbReader.nextInt(); if (guess == numToGuess) { device.out.println("superb"); ruin; } else { device.out.println("incorrect!"); if (( Math.abs(numToGuess - guess) ) >= 3){ device.out.println("chilly"); } else if (( Math.abs(numToGuess-guess) ) == 2){ device.out.println("heat"); } else if (( Math.abs(numToGuess-guess) ) == a million){ device.out.println("warm"); } }


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