重复的JOptionPane弹出?

时间:2016-09-28 17:57:43

标签: java if-statement

所以我正在编写一个程序,它给出了x秒的每个输入的分钟数...现在的问题是,一旦我输入第一个值,它就会要求另一个值并除以......还有另外一个......还有另一个...等等......我怎样才能让它只给我一个值并用一个值而不是一个永无止境的东西来完成?

import javax.swing.JOptionPane;

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

double seconds;
String input;

input = JOptionPane.showInputDialog("Enter any number of seconds");

seconds = Double.parseDouble(input);

if (seconds >= 60);
JOptionPane.showInputDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds.");


if (seconds >= 3600);
JOptionPane.showInputDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds.");

if (seconds >= 86400);
JOptionPane.showInputDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds.");

System.exit(0);




}
}

1 个答案:

答案 0 :(得分:0)

首先 - 在每个if语句后删除分号。其次,当您不要求输入时,将showInputDialog更改为showMessageDialog。第三,纠正代码的逻辑:

class TimeCalculator{

    public static void main(String[] args) {
        double seconds;
        String input;

        input = JOptionPane.showInputDialog("Enter any number of seconds");

        seconds = Double.parseDouble(input);

        if (seconds >= 60)
            JOptionPane.showMessageDialog(null, "There are " + (seconds / 60) + " minutes in " + seconds + " seconds.");

        if (seconds >= 3600)
            JOptionPane.showMessageDialog(null, "There are " + (seconds / 3600) + " hours in " + seconds + " seconds.");

        if (seconds >= 86400)
            JOptionPane.showMessageDialog(null,
                    "There are " + (seconds / 86400) + " days in " + seconds + " seconds.");
        System.exit(0);
    }
}