第二个JOptionPane.showInputDialog无法打开

时间:2015-10-14 04:17:19

标签: java swing

包minFinder; / *  *从用户获取两个值,并找到较小的值  * /

import java.util.Scanner;

导入javax.swing.JOptionPane;

public class minFinder {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    double num1, num2;

    JOptionPane.showInputDialog("Please input the first number");
    num1 = keyboard.nextDouble();

    JOptionPane.showInputDialog("Please input the second number");
    num2 = keyboard.nextDouble();

    JOptionPane.showInputDialog(Math.min(num1, num2));
}

}

这是令我不安的代码,无论出于何种原因,第二个和第三个对话框都无法打开,我可以得到一些帮助吗?我觉得解决方案可能很明显。

由于

2 个答案:

答案 0 :(得分:2)

  

无论出于何种原因,第二个和第三个对话框都不会打开,

扫描仪正在等待您从键盘输入数据。

摆脱Scanner类。如果您打算使用GUI,则不需要键盘输入。

有关使用JOptionPane的示例,请查看Getting Input From the User上Swing教程中的部分。

答案 1 :(得分:1)

扫描仪仅从控制台获取输入。输入对话框已经有一个接收输入的GUI,因此你可以摆脱扫描仪。

第二个和第三个对话框未显示的原因是因为第一个扫描程序仍在等待输入,即使某些文本已输入到输入对话框中。第一个是工作,因为扫描仪不等待任何输入。

这里有正确的代码:

package minFinder; /* * Takes two values from the User and and finds the smaller value */

import java.util.Scanner;

import javax.swing.JOptionPane;

public class minFinder {
    public static void main(String[] args) {
        double num1, num2;

        num1 = Double.parseDouble(JOptionPane.showInputDialog("Please input the first number"));
        num2 = Double.parseDouble(JOptionPane.showInputDialog("Please input the first number"));

        JOptionPane.showMessageDialog(null, Math.min(num1, num2)); //Note how I changed it to a message dialog
    }
}

您应该考虑的其他一些事情是类名应该以大写字母开头,而包名应该是完全小写的。

上面的代码实际上并没有检查输入的字符串是否为double,因此如果它是一个无效的数字,则会抛出NumberFormatException。解决这个问题的一种方法是执行以下操作:

package minFinder; /* * Takes two values from the User and and finds the smaller value */

import javax.swing.JOptionPane;

public class minFinder {
    public static void main(String[] args) {
        double num1 = 0;
        double num2 = 0;
        boolean invalidNumber;

        try {
            num1 = Double.parseDouble(JOptionPane.showInputDialog("Please input the first number"));
        } catch(NumberFormatException e) {
            invalidNumber = true;
            while(invalidNumber) {
                try {
                    num1 = Double.parseDouble(JOptionPane.showInputDialog("Invalid number. Please try again"));
                    invalidNumber = false;
                } catch(NumberFormatException e2) {}
            }
        }

        try {
            num2 = Double.parseDouble(JOptionPane.showInputDialog("Please input the second number"));
        } catch(NumberFormatException e) {
            invalidNumber = true;
            while(invalidNumber) {
                try {
                    num2 = Double.parseDouble(JOptionPane.showInputDialog("Invalid number. Please try again"));
                    invalidNumber = false;
                } catch(NumberFormatException e2) {}
            }
        }

        JOptionPane.showMessageDialog(null, Math.min(num1, num2));
    }
}

以下是有关对话框的更多信息:http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

相关问题