while循环不打印消息

时间:2015-10-29 01:49:12

标签: java loops while-loop

该程序是一个用户必须猜出要获胜的神奇数字的游戏。我是java的新手,我还在学习。当我输入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 == 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); */
    }
}

2 个答案:

答案 0 :(得分:2)

内部while循环不仅不需要,它还会破坏你的程序。两种方案userAnswer都是53。让我们检查这两种情况。

不等于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);
    }
}

我不知道您希望如何处理&#34;胜利&#34;,您可以在该比较中将stillplaying设置为false

我还冒昧地删除了与true外部while循环的比较,因为它什么也没做。将变量命名为lowerCamelCase =&gt;也是很常见的。 stillPlaying只能被称为playing,这是boolean游戏循环变量的一个相当常见的名称。

答案 1 :(得分:0)

您的代码

else if(userAnswer == 53){                     JOptionPane.showMessageDialog(null,“恭喜!你刚赢了5个布朗尼点!”);

永远不会达到

,因为你把它放在里面

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

只需用它的各个括号删除这个内部while循环,它应该是好的。

正确的代码将是:

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);


            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); */
}

}