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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need Help Desperatly!!!

    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"));
    }
    }

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    What have you attempted? What are you stuck at? As per the rules of these forums we are not going to do your homework for you. However if you tell us what you're stuck at, what errors you're having we can point you in the right direction, or at the least, help you solve that particular error.

    Also, please from now on use code blocks when posting code so we can see the indentation. That is [code ] [/ code] without the space.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry about the code tags thing i didnt realise, as for the problem i explained that the program doesnt seen to tell the user to guess higher or lower and i dont know why cos it seems okto me but im obviously missing somethink.

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Well let's look at what your code is doing when you look at the user's input:

    Code:
    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");
    }
    So, if the user_number does not equal 0, you output that the user got the answer correct!

    What you need to do is to take the game_number and compare it to the user_number. You do this just in the same way by using if statements. But instead of doing !=, you would use > <

    So something like ..

    Code:
    if(user_number > game_number)
    {
       // what the user guessed was greater, print that out
    }
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #5
    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
    To add to Aradon's suggestions, let's look at your code for getting the user's first guess.
    Code:
    do
    {
    System.out.printf("&#37;nEnter your guess, from 1 to 1000 inclusive (0 to quit):");
    user_number = data_input.nextInt();
    
    } while ((user_number >= 1) && (user_number <= 1000));
    In plain English this reads: "Keep getting user input until the user enters a number that is less than 1 or greater than 1000". This is the reason you're only seeing "guess higher/lower" only if you guess -1 or 1001. If you enter a valid guess, this loop will continue and ask for another guess, and not continue on to your section of code that checks to see if the guess was high or low.

    So, you'll want to rethink your logic here. I don't think you meant for this section of code to "loop" if the user entered a valid guess. Maybe you don't want a loop here at all, or maybe you want to rethink your while condition?

  • #6
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    thank you so much for your help gox, i couldnt believe i made such a simle logical mistake lol, i wont be making that mistake again thanks


  •  

    Posting Permissions

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