2015-10-29 88 views
0

该程序是一个游戏,用户必须猜测神奇号码才能赢。我是新来的Java,我仍然在学习。当我输入53时,它只是问问题而不显示输出。我确信这只是一个简单的错误,我只是没有抓住。谢谢!while loop not printing message

import javax.swing.JOptionPane; 
class NumberFrom1To100 { 
    public static void main(String[] args) { 

     boolean stillplaying = true; 
     double answer = 53; 
     double userAnswer; 
     String userInput; 

     while (stillplaying == true) { 

      userInput = JOptionPane.showInputDialog("Guess a number between 1 and 100."); 
      userAnswer = Double.parseDouble(userInput); 

      while (userAnswer != 53) { 
       if (userAnswer < 53) { 
        JOptionPane.showMessageDialog(null, "You picked a number LESS than the mystery number. Try again."); 
        break; 
       } else if (userAnswer > 53) { 
        JOptionPane.showMessageDialog(null, "You picked a number GREATER than the mystery number. Try again."); 
        break; 
       } else if (userAnswer == 53) { 
        JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!"); 
        break; 
       } 
      } 
     } 
     /* System.exit(0); */ 
    } 
} 
+3

而(stillplaying)是细别t需要==真正的你不需要另一个while循环... –

+0

你为什么使用'double'来存储答案? –

+0

当userAnswer为53时仍然播放为false,并且可以删除while(userAnswer!= 53)。 – PK20

回答

2

inner while循环不仅不是必需的,而且它也破坏了你的程序。两种情况下,userAnswer53或不。让我们来检查这两种情况。不等于53会触发while循环。如果它小于,等于(它永远不可能),或者大于53它会中断。这使得循环过时。

等于53根本不会触发循环,这是您现在遇到的结果。

import javax.swing.JOptionPane; 

class NumberFrom1To100 
{ 
    public static void main(String[] args) { 

     boolean stillplaying = true; 
     double answer = 53; 
     double userAnswer; 
     String userInput; 

     while (stillplaying) { 
      userInput = JOptionPane.showInputDialog("Guess a number between 1 and 100."); 
      userAnswer = Double.parseDouble(userInput); 

      if (userAnswer < 53) { 
       JOptionPane.showMessageDialog(null, "You picked a number LESS than the mystery number. Try again."); 
      } else if (userAnswer > 53) { 
       JOptionPane.showMessageDialog(null, "You picked a number GREATER than the mystery number. Try again."); 
      } else { 
       JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!"); 
      } 
     } 

     // System.exit(0); 
    } 
} 

我不知道你想怎么处理“胜利”,你可以在比较设置stillplayingfalse

我还采取了删除您的比较012iber在外部while循环的可用性,因为它什么都不做。使用lowerCamelCase =>stillPlaying来命名变量也是很常见的,它只能被称为playing,这是一个相当常见的boolean游戏循环变量。

0

您的代码

否则,如果(userAnswer == 53){ JOptionPane.showMessageDialog(NULL, “恭喜你刚刚赢得5加分!”);

永远不会达到的,因为你已经把它里面

而(userAnswer!= 53){} .....

只是删除这个内循环的同时与它的相应的括号,它应该是好。

正确的代码将是:

import javax.swing.JOptionPane; 

类NumberFrom1To100 { 公共静态无效的主要(字串[] args){

boolean stillplaying = true; 
    double answer = 53; 
    double userAnswer; 
    String userInput; 

    while (stillplaying == true) { 

     userInput = JOptionPane.showInputDialog("Guess a number between 1 and 100."); 
     userAnswer = Double.parseDouble(userInput); 


      if (userAnswer < 53) { 
       JOptionPane.showMessageDialog(null, "You picked a number LESS than the mystery number. Try again."); 

      } else if (userAnswer > 53) { 
       JOptionPane.showMessageDialog(null, "You picked a number GREATER than the mystery number. Try again."); 

      } else if (userAnswer == 53) { 
       JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!"); 

      } 

    }`` 
    /* System.exit(0); */ 
} 

}

+0

if语句中的break是完全字面的打破应用程序。答案中也有一些格式问题。 – Emz