英文:
Why will pressing 'false' not break this java loop?
问题
我已经尝试了中断和将 `==` 更改为 `!=`。这仅是一个逻辑错误,因为除了循环永远不会结束外,其他所有操作都有效。
import javax.swing.JOptionPane;
import java.lang.Math;
public class NumGuess {
    public static void main(String[] args) {
        boolean anotherGame = true;
        int random = (int) (Math.random() * (20 - 0 + 1) + 0);
        int guesses = 5;
        int userGuess = -1;
        anotherGame = true;
        int invGuess = 0;
        while (anotherGame == true) {
            guesses = 5;
            userGuess = -1;
            random = (int) (Math.random() * (20 - 0 + 1) + 0);
            invGuess = 0;
            while (random != userGuess && guesses > 0) {
                userGuess = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 0 and 20."));
                while (userGuess < 0 || userGuess > 20) {
                    System.out.println("Enter an integer between 0 and 20.");
                    userGuess = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 0 and 20."));
                    invGuess++;
                }
                guesses--;
                if (random > userGuess) {
                    System.out.println("Higher!");
                    System.out.println("You have " + guesses + " tries left.");
                } else if (random < userGuess) {
                    System.out.println("Lower!");
                    System.out.println("You have " + guesses + " tries left.");
                } else {
                    System.out.println("You guessed correctly!");
                }
            }
            if (guesses == 0 && random != userGuess) {
                System.out.println("You have run out of guesses! The correct number was " + random + " .");
            }
            System.out.println("You had " + invGuess + " invalid guesses.");
            int anotherGameInt = JOptionPane.showConfirmDialog(null,
                    "Play again?", "Please select",
                    JOptionPane.YES_NO_OPTION);
            if (anotherGameInt == 1) {
                anotherGame = true;
            } else {
                anotherGame = false;
            }
        }
    }
}
英文:
I have tried breaks and changing the == to a !=. This is a logic error purely, as everything works apart from the loop never ending.
import javax.swing.JOptionPane;
import java.lang.Math;   
public class NumGuess
{
public static void main(String [] args){
boolean anotherGame = true; 
int random = (int)(Math.random()*(20-0+1)+0);
int guesses = 5;
int userGuess = -1; 
anotherGame = true;
int invGuess = 0;
while(anotherGame == true){
guesses = 5;
userGuess = -1;
random = (int)(Math.random()*(20-0+1)+0);
invGuess = 0;
while(random != userGuess && guesses > 0){
userGuess = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 0 and 20."));
while (userGuess < 0 || userGuess > 20){
System.out.println("Enter an integer between 0 and 20.");
userGuess = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 0 and 20."));
invGuess++;
}
guesses--;
if (random > userGuess){
System.out.println("Higher!");
System.out.println("You have "+guesses+" tries left.");
}else if(random < userGuess){
System.out.println("Lower!");
System.out.println("You have "+guesses+" tries left.");
}else{
System.out.println("You guessed correctly!");
}
}
if (guesses == 0 && random != userGuess){
System.out.println("You have run out of guesses! The correct number was "+random+" .");
}
System.out.println("You had "+invGuess+ "invalid guesses.");
int anotherGameInt = JOptionPane.showConfirmDialog(null,
"Play again?", "Please select",
JOptionPane.YES_NO_OPTION);
if (anotherGameInt == 1){
anotherGame = true;
}else{
anotherGame = false;
}
}            
}
答案1
得分: 4
JOptionPane.showConfirmDialog(null,
"Play again?", "Please select",
JOptionPane.YES_NO_OPTION);
returns JOption.YES_OPTION (1) for Yes and JOption.NO_OPTION (0) for no.
You can verify that with a System.out.println(anotherGameInt); after the call
<details>
<summary>英文:</summary>
JOptionPane.showConfirmDialog(null,
"Play again?", "Please select",
JOptionPane.YES_NO_OPTION);
returns ```JOption.YES_OPTION``` (1) for Yes and ```JOption.NO_OPTION``` (0) for no.
You can verify that with a ```System.out.println( anotherGameInt );``` after the call
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论