im trying to get a program to run the way i want it to. It compiles fine but it isnt performing things correctly. The program is a number generator game which the computer generates and then the user has to guess. In order for the user to guess the number there needs to be some intructions i.e. "your guess needs to be higher/lower".
These instructions are not outputted to the user therefore it is virtually impossible for the user to find this number.
The value is 1 to 1000 and anything outside this boundary displays the higher/lower message
I think it may be something to do with the while loop "
The full code is
any form of feedback would be appreciated
These instructions are not outputted to the user therefore it is virtually impossible for the user to find this number.
The value is 1 to 1000 and anything outside this boundary displays the higher/lower message
I think it may be something to do with the while loop "
Code:
} while ((user_number > 0) && (user_number <= 1000));
Code:
import java.util.*;
public class game
{
public static void main(String[] args)
{
// Declare variables, setup keyboard input and the
// random number generator
int game_number, user_number;
String continue_pref; //; not previously there
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 > 0) && (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 == game_number)
{
// User has guessed right
System.out.printf("%nYou guessed correctly, well done.%nDo you want to play again (y/Y)=Yes: or (n/N) No: ");
continue_pref = new String(data_input.next());
}
else
{
// User wants to quit
continue_pref = new String("No");
}
} while (continue_pref.equalsIgnoreCase("Y"));
}
}
any form of feedback would be appreciated