trustcompany3000
2008-10-05 16:05:59 UTC
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!");
}
}