Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 为什么我的程序在单击按钮后会给出错误的输出?_Java_Joptionpane - Fatal编程技术网

Java 为什么我的程序在单击按钮后会给出错误的输出?

Java 为什么我的程序在单击按钮后会给出错误的输出?,java,joptionpane,Java,Joptionpane,在我的程序中,我使用JOptionPane.showConfirmDialog来提示用户是否确定要退出模拟;如果您单击“否”,prgoram应打印“继续模拟…”并继续执行程序,但我发现,当您单击“否”时,它打印“继续模拟”,然后直接在“将不执行维护,退出模拟…”之后打印 你能帮我找出为什么会这样吗 if(main.equalsIgnoreCase("N") | main.equalsIgnoreCase("NO")){ result = J

在我的程序中,我使用JOptionPane.showConfirmDialog来提示用户是否确定要退出模拟;如果您单击“否”,prgoram应打印“继续模拟…”并继续执行程序,但我发现,当您单击“否”时,它打印“继续模拟”,然后直接在“将不执行维护,退出模拟…”之后打印 你能帮我找出为什么会这样吗

            if(main.equalsIgnoreCase("N") | main.equalsIgnoreCase("NO")){
                result = JOptionPane.showConfirmDialog(null, "WARNING! Are you sure you don't want to carry out maintenance? The simulation will stop if you click YES.  ", "TERMINATE THE SIMULATION? ", JOptionPane.YES_NO_OPTION);
                if(JOptionPane.NO_OPTION == result){
                    System.out.println("Continuing Simulation...");
                }
                if (JOptionPane.YES_OPTION == result) {
                    Result2 = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the simulation?", "Are You Sure?", JOptionPane.YES_NO_OPTION);
                } 
                if(JOptionPane.YES_OPTION == Result2){
                    System.out.println("Maintenance will not be carried out, exiting Simulation...");
                    try{
                        System.exit(0);
                    }
                    finally{
                        System.err.println("Exiting...");
                        System.exit(1);
                    }
                }
                if (JOptionPane.NO_OPTION == Result2) {
                    System.out.println("Continuing simulation...");
            }
            }
如果在第一个对话框中单击“否”,则不会为
Result2
分配任何值,因为您没有显示第二个对话框。但是,您仍然在这个if语句中使用
Result2
的值
if(JOptionPane.YES\u OPTION==Result2){

问题是,如果
Result2
是一个实例变量,而您尚未为其赋值,那么它的类型
int
的默认值为零

常量
JOptionPane.YES\u选项
的值也为零。因此if语句的计算结果为
true
,它打印维护消息并退出

您应该嵌套if语句,以便仅在为
Result2
分配了有意义的值时才执行对其值的检查,如下所示:

if (JOptionPane.YES_OPTION == result) {
    Result2 = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the simulation?", "Are You Sure?", JOptionPane.YES_NO_OPTION);
    if(JOptionPane.YES_OPTION == Result2){
        System.out.println("Maintenance will not be carried out, exiting Simulation...");
        try{
            System.exit(0);
        }
        finally{
            System.err.println("Exiting...");
            System.exit(1);
        }
    }
    if (JOptionPane.NO_OPTION == Result2) {
        System.out.println("Continuing simulation...");
    }
}
试试这个:

if(main.equalsIgnoreCase("N") | main.equalsIgnoreCase("NO")){
            result = JOptionPane.showConfirmDialog(null, "WARNING! Are you sure you don't want to carry out maintenance? The simulation will stop if you click YES.  ", "TERMINATE THE SIMULATION? ", JOptionPane.YES_NO_OPTION);
            if(JOptionPane.NO_OPTION == result){
                System.out.println("Continuing Simulation...");
            }else{
            if (JOptionPane.YES_OPTION == result) {
                Result2 = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit the simulation?", "Are You Sure?", JOptionPane.YES_NO_OPTION);
            }
            if(JOptionPane.YES_OPTION == Result2){
                System.out.println("Maintenance will not be carried out, exiting Simulation...");
                try{
                    System.exit(0);
                }
                finally{
                    System.err.println("Exiting...");
                    System.exit(1);
                }
            }
            if (JOptionPane.NO_OPTION == Result2) {
                System.out.println("Continuing simulation...");
            }
        }}
如果(main.equalsIgnoreCase(“N”)| main.equalsIgnoreCase(“NO”))正在使用按位包含或运算符“|”。请尝试使用逻辑或运算符“| |”,而不是:

if(main.equalsIgnoreCase("N") || main.equalsIgnoreCase("NO")){