Hi i am fairly new to java and am having a problem with some code, the
code was not origanlly designed by me it is for a piece of work i am
doing, and i am trying to modify it to get it to work, the problem is
the user isprompted for a number between 1 - 1000, if they guess too
high they are supposed to get a message saying guess lower and
viseversa. only the guess higher and guess lower messages dont disply
it just asks the user for a new number, strangly enough the guess
higher and guess lower only show up if i go beyond the confines of the
program for instance if i enter -1 it will tell me to guess higher and
if i guess 1001 it will tell me to guess lower. can someone please help
me with this. the code is below:
import java.util.*;
public class guessgameb
{
public static void main(String[] args)
{
// Declare variables, setup keyboard input and the
// random number generator
int game_number, user_number;
String continue_pref;
Scanner data_input = new Scanner(System.in);
Random generate = new Random();
do
{
// Generate game number
game_number = generate.nextInt(999) + 1;
// The following line is a debug line, comment out
// for real game.
System.out.printf("Game number:%d%n", game_number);
// Get users first guess
System.out.print("The computer has generated a number.");
do
{
System.out.printf("%nEnter your guess, from 1 to 1000 inclusive (0 to quit):");
user_number = data_input.nextInt();
} while ((user_number >= 1) && (user_number <= 1000));
// While user has not guessed right and does not want to quit
while ((user_number != game_number) && (user_number != 0))
{
if (user_number < game_number)
{
System.out.printf("You need to guess higher%n");
}
else
{
System.out.printf("You need to guess lower%n");
}
// Get users next guess
do
{
System.out.printf("%nEnter your guess, from 1 to 1000 inclusive (0 to quit):");
user_number = data_input.nextInt();
} while ((user_number >= 0) && (user_number <= 1000));
}
if (user_number != 0)
{
// User has guessed right
System.out.printf("%nYou guessed correctly, well done.%nDo you want to play again (y/Y)=Yes: ");
continue_pref = new String(data_input.next());
}
else
{
// User wants to quit
continue_pref = new String("No");
}
} while (continue_pref.equalsIgnoreCase("N"));
}
}
Enter your message below
Sign in or Join us (it's free).