Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java Programming help

Posted on 2003-12-08
2
Medium Priority
?
242 Views
Last Modified: 2010-03-31
Hello everyone,
i am working on a little program where i put in a random genenrator that comes up with a number between 1 and 100 and the user has to guess what the number is. Whether or not the user gets the question wrong [by either guessing too high or too low] or right, a diffrent response comes up [and if the user is wrong, he/she has to guess again]. I got the whole program written but it does'nt do what i want it too do, it always asks the user to guess again if he/she is wrong, however, it only shows if the user guessed too high or too low once. I know that this problem is related to the fact that i have my if statements written in the wrong order and i don't have some sort of statement that automaticalyl restarts a program if some sort of condition is met. So all I am asking is a solution to this problem as well as soem measures on how not to do this again [because i know i could end up doing this again very easily]. Thanks ahead of time and here is the code that I got so far:

      KeyboardReader reader = new KeyboardReader();      
int GuessNumber;
int Guess;


      GuessNumber = generator.nextInt(100);
      
      System.out.println(GuessNumber);
      Guess = reader.readInt("Please put in your guess: ");
      
      if (Guess == GuessNumber){
            System.out.println("You are right, you talented being you ;)");
      }else{      
      if (Guess < GuessNumber){
            System.out.println("You guessed too low, you must reach higher for the stars");
      while (Guess != GuessNumber){
            Guess = reader.readInt("Please put in your guess: ");
            
      }
      }else{
      }
      }
      if (Guess > GuessNumber){
            System.out.println("You guessed too high, you must go lower as if you were playing Limbo");
            while (Guess != GuessNumber){
                        Guess = reader.readInt("Please put in your guess: ");
      }
      }
      
      
      
      }
}
0
Comment
Question by:BUpYo
2 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9902266
try something like:

while (Guess != GuessNumber){
   Guess = reader.readInt("Please put in your guess: ");
    if (Guess == GuessNumber){
          System.out.println("You are right, you talented being you ;)");
     }else if (Guess < GuessNumber){
          System.out.println("You guessed too low, you must reach higher for the stars");
     }
     else if (Guess > GuessNumber){
          System.out.println("You guessed too high, you must go lower as if you were playing Limbo");
     }
}
0
 

Accepted Solution

by:
javaTL earned 200 total points
ID: 9902326
The problem with the code is that the 'lower' and 'upper' checks are done outside the while loop for all values besides the first. Only the 'equals' check is done in the while loop.

To solve the problem use the following code:

    KeyboardReader reader = new KeyboardReader();    
    int GuessNumber;
    int Guess;

     GuessNumber = generator.nextInt(100);
     
     System.out.println(GuessNumber);
     Guess = reader.readInt("Please put in your guess: ");
     
     while (Guess != GuessNumber) {
            if (Guess < GuessNumber) {
          System.out.println("You guessed too low, you must reach higher for the stars");
            } else {
                    //else case always checks for high, since number is not equal & not low
          System.out.println("You guessed too high, you must go lower as if you were playing Limbo");
            }
      Guess = reader.readInt("Please put in your guess: ")
      }
      //Execution flow reaches here when numbers are equal
      System.out.println("You are right, you talented being you ;)");

Regards,
javaTL
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question