Solved

Java Programming help

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
array6 challenfge 6 63
Error with Java/Cache JDBC Classpath 2 20
Cipher Configuration on Apache HTTPD 4 20
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

706 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

15 Experts available now in Live!

Get 1:1 Help Now