Hi all,
Having issues with my guessing game I made. For once its not a compiling error. For some reason my loop only executes once, and it wont re-execute when the wrong number is entered. Hoping someone can help me. Here is my code:

import javax.swing.*; 
public class Guessing Game
{
  public static void main(String[] args) 
  {
    String inputString;
    int randomNumber = (int)(Math.random() * 100);
      // gets random value betwween 0 and 100
    int inputNumber;
     boolean isCorrect = false;
     inputString = JOptionPane.showInputDialog(null,
         "Guess my number between 0 and 100");
      inputNumber = Integer.parseInt(inputString);
      //changed correct to isCorrect
      while(isCorrect = false)
      inputString = JOptionPane.showInputDialog(null,
         "Guess my number between 0 and 100");
      inputNumber = Integer.parseInt(inputString);
        // changed => to ==
      if(inputNumber == randomNumber)
        {
          JOptionPane.showMessageDialog(null, "Yes! Great!");
        isCorrect = true;
        }
        
        //put if on one line and added <
         else if(inputNumber > randomNumber)
        {
           JOptionPane.showMessageDialog(null, 
               "Too high! Try again");
        }
        else
        {
            JOptionPane.showMessageDialog(null, 
               "Too low! Try again.");
                    isCorrect = false;
    }
     System.exit(0);
  }
}

Thanks much,
Sam

wont re-execute when the wrong number is entered.

What controls the re-execution of the loop? Where is the value set to allow the loop to exit? If you can't see where your logic problem is by playing computer with a piece of paper and pencil, Try adding some print outs at every place you change the value of the controlling variable to show you where you are not setting it correctly.

Also look at the formatting and indentation levels of your code. It makes it easier to read if code within {} blocks are indented from code outside those blocks.
If you'll look at this carefully, you could find your problem.

hey Kramerd,
if i change the = to == it puts me into an endless loop of the guess a number input. I had it that way previously. and Norm i cant pencil and paper it, that wouldnt get me anywhere, im a trial and error kind of person, if i wrote it down there would be no trial involved in it. I tried print outs but im not compiling through a cmd prompt so it doesnt print values unless i put it at the end of all my code.

also as far as i can see and understand the while loop should continue to loop until isCorrect is changed to true which happens when the user guesses correctly which is contained in the first if statement. I tell it in the other if that if the value isnt correct that isCorrect is false which i was hoping would tell it to start the loop over but obviously that didnt work, ive been working on this for the past 2 hours today and a few hours yesterday. Im no pro but im trying my best.

Sam

The = does need to be == instead. But you also have a problem with your curly braces: { and } need to be around everything that should be inside the while loop.

while (condition) {
    // loop body
  }

What about all the if curly braces? doesnt the if statement itself have an opening and closing curly, along with the else statement? That part is confusing. I more used to visual basic which uses no curly braces so its hard to grasp that concept of it.

i'm a trial and error kind of person, if i wrote it down there would be no trial involved in it.

Interesting approach. Sort of like the 1000 monkeys at typewriters will eventually write all books ever written.

You are very quickly going to hit a brick wall. Unless you have the 1000 monkeys to try all the ways to write a program.

You MUST design the program BEFORE you write the code. Otherwise you're wasting everyones time.

if i was only taking random guesses it would take forever, but with educated assumptions as i would call i can get rather close and with a small push in the right direction could figure this out and isnt really wasting anyones time. You said yourself if i looked close i could figure it out so i must not be that far off. Its mostly all written with the design, my lack of complete knowledge of loops has nothing to do with the design of the code, its a lack of knowledge.

Norm i dont mean to sound rude, i just learn by doing, its my learning style, i do apreciate the help, but i really wouldnt get any where by writing anything down.

just doing has gotten me by so far.
Im trying to learn some programming, i have a book with exercises and stuff. Im trying to look stuff up and trying to do this but java is much different than VB so im having issues.

What about all the if curly braces? doesnt the if statement itself have an opening and closing curly, along with the else statement? That part is confusing. I more used to visual basic which uses no curly braces so its hard to grasp that concept of it.

Yes, the if and else statements can have curly braces too. For example,

while (condition1) {
   if (condition2) {
     // do some stuff
   } else {
     // do some other stuff
   } // ends else
} // ends while

Edited 6 Years Ago by kramerd: n/a

ok i got it and will post my results, but can i ask, what is the purpose of the "system.exit" part of code? I took that out and suddenly it works fine. I looks as if it ends the program before it gets the chance to re-execute the loop. Is that right?
Here is what i have:

import javax.swing.*; 
public class Guessing Game
{
  public static void main(String[] args) 
  {
    String inputString;
    int randomNumber = (int)(Math.random() * 100);
      // gets random value betwween 0 and 100
    int inputNumber;
     boolean isCorrect = false;
     //changed correct to isCorrect
      while(isCorrect == false)
        {
      inputString = JOptionPane.showInputDialog(null,
         "Guess my number between 0 and 100");
      inputNumber = Integer.parseInt(inputString);
        // changed => to ==
      if(inputNumber == randomNumber)
        {
          JOptionPane.showMessageDialog(null, "Yes! Great!");
        isCorrect = true;
        }
        
        //put if on one line and added <
         else if(inputNumber > randomNumber)
        {
           JOptionPane.showMessageDialog(null, 
               "Too high! Try again");
        }
        else
        {
            JOptionPane.showMessageDialog(null, 
               "Too low! Try again.");
                    isCorrect = false;
    }
     
  }
}
}

Yes, the purpose of System.exit is to end the program. If you had the System.exit after the closing curly brace of the while loop, it wouldn't hurt anything to keep it there, but it doesn't server much purpose either. It is usually not needed in a main method, since when the main method ends the program usually ends as well. But there are times when you need it, so it's nice to know you can use it if you ever do.

Well initially as you can see in the first time I posted my code I had it right after the first closing brace at the end of the code, which as I think of it now would be the closing if right? I think I tried moving it after the second closing brace once and it wouldn't compile then. Good to know what the purpose is. Thanks so much kramerd, its kinda hard trying to learn this without someone to teach you so having someone that can explain things as you have is great.

Thanks again,
Sam

This question has already been answered. Start a new discussion instead.