Need Help Desperatly!!!

  • 13 years ago

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

  • 13 years ago

    hi i couldn't understand ur code properly but if u want to do wat u hav written i.e showing go higher or go lower den i guess jus by using if slese loop u can do it..

    if u still need help den contact me on [email protected] 

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In theory, theory and practice are the same. In practice, they're not.”