Solved

Java Programming help

Posted on 2003-12-08
2
219 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 62
custom annotations 9 32
even odd program using while loop 3 29
servlet filter example 37 44
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now