简体   繁体   中英

Java while loop issues

Im brand new to java, and I'm writing this short program where you guess a number between 1 and 10. The correct number is stored as an integer. If you guess a lower number, its supposed to say "The correct number is higher", and if you guess a higher it should say "The correct number is lower". Here's what I have:

    import javax.swing.*;

public class Oppg3 {
    public static void main(String[] args) {
        int number = 7;
        int guessed = Integer.parseInt(JOptionPane.showInputDialog("Guess a number between 1 and 10"));
        while(guessed>number) guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a lower number."));
        while(guessed<number) guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a higher number."));
        JOptionPane.showMessageDialog(null, guessed + " is correct!");

    }

}

So obviously that wont work, cause if you enter a lower number it'll jump to the next one, and that will come up as correct even if its higher. So how do I fix this so that it'll check for both statements? Sorry for bad explaining. Thanks.

The simplest solution is to replace your current whiles with if s, and contain them both in a

while(guessed!=number)
{
    if(guessed>number) //stuff
    if(guessd<number) //otherstuff
}

If I where you I would do the following:

  • I would use a do while loop.
  • In the method body, I would check to see if the user guessed the number.
  • If yes, congratulate the user and the loop exits.
  • Else if the number is higher, I will inform the user accordingly and allow him to give another try.
  • Else, I will tell the user the number is lower and will let him go on another try.

As is, if your user gives a number which is larger on the second loop it should tell the user that the number is correct, regardless of the value, which is not the desirable behaviour.

Instead of two while loops, create one loop that goes around as long as the guessed number is not equal to the correct answer. Inside the loop, create if statements to check if the guessed number is higher or lower than the correct answer, and do the appropriate actions.

尝试对两个语句以及if子句分别使用一个while循环。

Try this:

While (ReadNumber != guessed)
{
  //if Higher then ...show appropriate message
  //if lower then ...show appropriate message
  //ReadNumber = read Input again
}

Replace your final line with this.

if(guessed == number){
    JOptionPane.showMessageDialog(null, guessed + " is correct!");
}

Or change it all,

bool correct = false;
while(correct == false)
{
     if(guessed == number)
     {
      enter code here
      correct = true;
     }
     else if(guessed > number)
     {
     enter code here
     }
     //check for guessed < number
}

You have to use the while to loop while the user don't guess the correct number. Inside the loop you have to check if the number is higher or lower.

So the program would look something like this:

while (the user don't guess the number){
//
if (the guessed number is higher){...}
if (the guessed number is lower){...}
//
}

The logic is incorrect. You want a user to reenter a number every time he's wrong. That's why your loop is going to look like while(guessed != number) { ... } . Inside, you need to accept the input, validate and give an output.

I won't put the right code here, cause it's better to discover it yourself for you.

Use an outer while loop, with condition, if the guessed number is not equal to the actual number. In the body of the while clause you split the cases upper and lower with an if statement. The whole code would look something like this:

import javax.swing.*;

public class Oppg3 {
    public static void main(String[] args) {
        int number = 7;
        int guessed = Integer.parseInt(JOptionPane.showInputDialog("Guess a number between 1 and 10"));
        while(guessed!=number) {
            if(guessed>number) guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a lower number."));
            else guessed = Integer.parseInt(JOptionPane.showInputDialog(guessed + " is wrong. Try a higher number."));
        } 
        JOptionPane.showMessageDialog(null, guessed + " is correct!");

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM