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

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

Why will pressing 'false' not break this java loop?

问题

  1. 我已经尝试了中断和将 `==` 更改为 `!=`这仅是一个逻辑错误因为除了循环永远不会结束外其他所有操作都有效
  2. import javax.swing.JOptionPane;
  3. import java.lang.Math;
  4. public class NumGuess {
  5. public static void main(String[] args) {
  6. boolean anotherGame = true;
  7. int random = (int) (Math.random() * (20 - 0 + 1) + 0);
  8. int guesses = 5;
  9. int userGuess = -1;
  10. anotherGame = true;
  11. int invGuess = 0;
  12. while (anotherGame == true) {
  13. guesses = 5;
  14. userGuess = -1;
  15. random = (int) (Math.random() * (20 - 0 + 1) + 0);
  16. invGuess = 0;
  17. while (random != userGuess && guesses > 0) {
  18. userGuess = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 0 and 20."));
  19. while (userGuess < 0 || userGuess > 20) {
  20. System.out.println("Enter an integer between 0 and 20.");
  21. userGuess = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 0 and 20."));
  22. invGuess++;
  23. }
  24. guesses--;
  25. if (random > userGuess) {
  26. System.out.println("Higher!");
  27. System.out.println("You have " + guesses + " tries left.");
  28. } else if (random < userGuess) {
  29. System.out.println("Lower!");
  30. System.out.println("You have " + guesses + " tries left.");
  31. } else {
  32. System.out.println("You guessed correctly!");
  33. }
  34. }
  35. if (guesses == 0 && random != userGuess) {
  36. System.out.println("You have run out of guesses! The correct number was " + random + " .");
  37. }
  38. System.out.println("You had " + invGuess + " invalid guesses.");
  39. int anotherGameInt = JOptionPane.showConfirmDialog(null,
  40. "Play again?", "Please select",
  41. JOptionPane.YES_NO_OPTION);
  42. if (anotherGameInt == 1) {
  43. anotherGame = true;
  44. } else {
  45. anotherGame = false;
  46. }
  47. }
  48. }
  49. }
英文:

I have tried breaks and changing the == to a !=. This is a logic error purely, as everything works apart from the loop never ending.

  1. import javax.swing.JOptionPane;
  2. import java.lang.Math;
  3. public class NumGuess
  4. {
  5. public static void main(String [] args){
  6. boolean anotherGame = true;
  7. int random = (int)(Math.random()*(20-0+1)+0);
  8. int guesses = 5;
  9. int userGuess = -1;
  10. anotherGame = true;
  11. int invGuess = 0;
  12. while(anotherGame == true){
  13. guesses = 5;
  14. userGuess = -1;
  15. random = (int)(Math.random()*(20-0+1)+0);
  16. invGuess = 0;
  17. while(random != userGuess &amp;&amp; guesses &gt; 0){
  18. userGuess = Integer.parseInt(JOptionPane.showInputDialog(&quot;Enter a number between 0 and 20.&quot;));
  19. while (userGuess &lt; 0 || userGuess &gt; 20){
  20. System.out.println(&quot;Enter an integer between 0 and 20.&quot;);
  21. userGuess = Integer.parseInt(JOptionPane.showInputDialog(&quot;Enter a number between 0 and 20.&quot;));
  22. invGuess++;
  23. }
  24. guesses--;
  25. if (random &gt; userGuess){
  26. System.out.println(&quot;Higher!&quot;);
  27. System.out.println(&quot;You have &quot;+guesses+&quot; tries left.&quot;);
  28. }else if(random &lt; userGuess){
  29. System.out.println(&quot;Lower!&quot;);
  30. System.out.println(&quot;You have &quot;+guesses+&quot; tries left.&quot;);
  31. }else{
  32. System.out.println(&quot;You guessed correctly!&quot;);
  33. }
  34. }
  35. if (guesses == 0 &amp;&amp; random != userGuess){
  36. System.out.println(&quot;You have run out of guesses! The correct number was &quot;+random+&quot; .&quot;);
  37. }
  38. System.out.println(&quot;You had &quot;+invGuess+ &quot;invalid guesses.&quot;);
  39. int anotherGameInt = JOptionPane.showConfirmDialog(null,
  40. &quot;Play again?&quot;, &quot;Please select&quot;,
  41. JOptionPane.YES_NO_OPTION);
  42. if (anotherGameInt == 1){
  43. anotherGame = true;
  44. }else{
  45. anotherGame = false;
  46. }
  47. }
  48. }

答案1

得分: 4

  1. JOptionPane.showConfirmDialog(null,
  2. "Play again?", "Please select",
  3. 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

  1. <details>
  2. <summary>英文:</summary>

JOptionPane.showConfirmDialog(null,
"Play again?", "Please select",
JOptionPane.YES_NO_OPTION);

  1. returns ```JOption.YES_OPTION``` (1) for Yes and ```JOption.NO_OPTION``` (0) for no.
  2. You can verify that with a ```System.out.println( anotherGameInt );``` after the call
  3. </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:

确定