Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help desperately deadlines 2marrow

    I am currently stuck this is the first time i have ever used java an it is wrecking my brains i dont understand whats wrong with it. It all works rite up to the point were it wont congradulate the use for getting the number right could anyone please please tell me whats wrong. I dnt want the answer just need a step in the right direction

    Here is the code

    import java.util.*;

    public class guessgame
    {
    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 >= game_number) && (user_number <= game_number));

    // 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("%nYou need to guess higher%n");
    else
    System.out.printf("%nYou 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 >= game_number) && (user_number <= game_number));
    }

    {
    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: ");
    continue_pref = new String("Yes");//(data_input.next());
    }

    {
    if (user_number == 0)
    // User wants to quit
    continue_pref = new String("No");
    }
    } while (continue_pref.equalsIgnoreCase("N"));
    }
    }

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    I didn't run your code, but it looks like the logic of your while-loop is incorrect to me.
    Code:
    // While user has not guessed right and does not want to quit
    while ((user_number == game_number) || (user_number != 0))
    {
    ...
    This reads "Do this set of code (get user guess) while the user has guessed the right number or the user doesn't want to quit". I think you meant this condition to be "while the user hasn't guessed the right number or the user doesn't want to quit". So, maybe you want the following instead?
    Code:
    // While user has not guessed right and does not want to quit
    while ((user_number != game_number) || (user_number != 0))
    {
    ...
    See if that helps.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •