Solved

Java Programming help

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

631 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