为什么按下’false’不会中断这个Java循环?

huangapple go评论67阅读模式
英文:

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 &amp;&amp; guesses &gt; 0){
userGuess = Integer.parseInt(JOptionPane.showInputDialog(&quot;Enter a number between 0 and 20.&quot;));
while (userGuess &lt; 0 || userGuess &gt; 20){
System.out.println(&quot;Enter an integer between 0 and 20.&quot;);
userGuess = Integer.parseInt(JOptionPane.showInputDialog(&quot;Enter a number between 0 and 20.&quot;));
invGuess++;
}
guesses--;
if (random &gt; userGuess){
System.out.println(&quot;Higher!&quot;);
System.out.println(&quot;You have &quot;+guesses+&quot; tries left.&quot;);
}else if(random &lt; userGuess){
System.out.println(&quot;Lower!&quot;);
System.out.println(&quot;You have &quot;+guesses+&quot; tries left.&quot;);
}else{
System.out.println(&quot;You guessed correctly!&quot;);
}
}
if (guesses == 0 &amp;&amp; random != userGuess){
System.out.println(&quot;You have run out of guesses! The correct number was &quot;+random+&quot; .&quot;);
}
System.out.println(&quot;You had &quot;+invGuess+ &quot;invalid guesses.&quot;);
int anotherGameInt = JOptionPane.showConfirmDialog(null,
&quot;Play again?&quot;, &quot;Please select&quot;,
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>

huangapple
  • 本文由 发表于 2020年8月31日 11:56:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63664591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定