Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 第二个JOptionPane.showInputDialog未打开_Java_Swing - Fatal编程技术网

Java 第二个JOptionPane.showInputDialog未打开

Java 第二个JOptionPane.showInputDialog未打开,java,swing,Java,Swing,包装取样器; /* *从用户处获取两个值,然后查找较小的值 */ 导入java.util.Scanner 导入javax.swing.JOptionPane 公共类minFinder{ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double num1, num2; JOptionPane.showInputDialog("Please input

包装取样器; /* *从用户处获取两个值,然后查找较小的值 */

导入java.util.Scanner

导入javax.swing.JOptionPane

公共类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));
}
}

这是困扰我的代码,无论出于什么原因,第二个和第三个对话框都不会打开,我能得到一些帮助吗?我觉得解决办法很明显

谢谢

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

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

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


查看Swing教程中关于使用
作业窗格的示例部分,扫描仪仅从控制台获取输入。输入对话框已经有一个接受输入的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
    }
}
你应该考虑的是,类名应该从大写字母开始,包名应该是完全小写的。 上面的代码实际上并没有检查输入的字符串是否是双精度的,因此如果它是无效的数字,将抛出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));
    }
}

这里有一些关于对话框的更多信息:

我假设您将进行更新,以显示OP应该如何使用
inputDialog
,所以我现在只想+1为什么您要使用
inputDialog
并要求用户通过命令行输入值?这没有道理