Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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
Loops JOptionPane和While循环(用户输入添加)_Loops_While Loop_Joptionpane_Addition - Fatal编程技术网

Loops JOptionPane和While循环(用户输入添加)

Loops JOptionPane和While循环(用户输入添加),loops,while-loop,joptionpane,addition,Loops,While Loop,Joptionpane,Addition,我是JAVA的初学者 扫描仪输入似乎很容易理解,但由于某些原因,我在使用JOptionPane时遇到了困难 不管怎么说,我的问题是…即使我得到了正确的答案,我还是会不断得到答案,然后再试一次 你能告诉我我做错了什么吗 您永远不会更新答案 改变这个 package iCanDoIt; import java.util.Scanner; import javax.swing.JOptionPane; public class Practice {

我是JAVA的初学者

扫描仪输入似乎很容易理解,但由于某些原因,我在使用JOptionPane时遇到了困难

不管怎么说,我的问题是…即使我得到了正确的答案,我还是会不断得到答案,然后再试一次

你能告诉我我做错了什么吗

您永远不会更新答案

改变这个

    package iCanDoIt;

    import java.util.Scanner;

    import javax.swing.JOptionPane;

    public class Practice {

        public static void main(String[] args) {

        String msg=JOptionPane.showInputDialog("Enter a number");
        int num1=Integer.parseInt(msg);
        String msg2=JOptionPane.showInputDialog("enter another number");
        int num2=Integer.parseInt(msg2);
        int addition=num1+num2;

        String msg3=JOptionPane.showInputDialog("What is" + num1 + "+ " + num2 + " ? ");

        int answer=Integer.parseInt(msg3);

        while(num1+num2!=answer){

            JOptionPane.showMessageDialog(null,"try again");
            String answer2=JOptionPane.showInputDialog("wrong. what is " + num1 + "+" + num2 + "?");
            int answer3=Integer.parseInt(answer2);

        if (num1+num2==answer3)

            JOptionPane.showMessageDialog(null,"GREAT job");


        }
        System.exit(0);

        }


    }
对此

int answer3=Integer.parseInt(answer2);
if (num1+num2==answer3)
您还可以通过将最后一个消息对话框移到循环之外,来摆脱if num1+num2==答案检查:

answer=Integer.parseInt(answer2);
if (num1+num2==answer)
    int answer=Integer.parseInt(msg3);

    while(num1+num2!=answer){

        JOptionPane.showMessageDialog(null,"try again");
        String answer2=JOptionPane.showInputDialog("wrong. what is " + num1 + "+" + num2 + "?");
        answer=Integer.parseInt(answer2);

    }
    JOptionPane.showMessageDialog(null,"GREAT job");
    System.exit(0);