Solved

Java Programming help

Posted on 2003-12-08
2
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Certificat to iSeries KeyStore 1 31
eclipse buid path vs tomcat lib path 10 49
jboss 7.1 start up error 1 85
Where to place postgres JDBC driver jar on tomcat 8 71
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…
Suggested Courses

752 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