简体   繁体   中英

Repeated JOptionPane pop-up?

so I'm writing a program that gives the number of minutes for every input of x seconds... now the issue is that once I type in the first value, it asks for another value and divides that....and another value...and another...and so on... how can I get it to only give me one value and finish with that one value instead of a never-ending thing?

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




}
}

First of all - remove the semicolons after each if statement. Secondly, change the showInputDialog to showMessageDialog when you are not asking for input. Thirdly, correct the logic of your code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM