为什么这个循环不能正常运行

我想编写一个 java 程序,在其中询问用户他们想要回答多少数学问题,并使用任何选择循环根据他们的答案生成随机问题,并记录他们回答正确的数量。我得到它来生成随机数学问题,但它只在它似乎跳过循环时才这样做。任何人都可以帮忙吗?


import javax.swing.JOptionPane;

import java.util.Random;

import java.util.Scanner;


/**

 *

 * @author user

 */

public class MathQuiz {


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Random obj = new Random();

        int num1 = obj.nextInt(10);

        int num2 = obj.nextInt(10);

        int rand = num1 + num2;

        String response = JOptionPane.showInputDialog(null,"How many problems would you like to solve?");

        int ans = Integer.parseInt(response); // answer from question

        String result= null;

        int times = input.nextInt();

        int counter = 0; //counts total math problems


        while (counter != ans){

            counter++;

            JOptionPane.showInputDialog(num1 + "+" +num2);

            if (ans == rand){

                result= "Correct";

            }else {

                result= "Incorrect";

            }

        }   JOptionPane.showMessageDialog(null, );


        }

}  


哆啦的时光机
浏览 132回答 2
2回答

catspeake

您在循环中获得的数字不应与您在 while(...) 条件中使用的数字存储在同一变量中。在这种情况下,您使用了 ans。在下面的示例中,我为数学答案和循环中迭代的次数设置了单独的变量。您遇到的另一个问题是您没有将结果消息传递给 showMessageDialog(..) 方法。import javax.swing.JOptionPane;import java.util.Random;import java.util.Scanner;public class MathQuiz {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        Random obj = new Random();        String timesString = JOptionPane.showInputDialog(null,"How many problems would you like to solve?");        int timesInt = Integer.parseInt(timesString); // answer from question        int counter = 0; //counts total math problems        while (counter != timesInt) {            counter++;            int num1 = obj.nextInt(10);            int num2 = obj.nextInt(10);            int rand = num1 + num2;            String answerString = JOptionPane.showInputDialog(num1 + "+" +num2);            int answerInt = Integer.parseInt(answerString);            JOptionPane.showMessageDialog(null, answerInt == rand ? "Correct" : "Incorrect");        }    }}

红糖糍粑

以下是您的代码中的问题: 计算值后没有显示结果。不是每次都生成随机数。&nbsp; &nbsp; public class MathQuiz {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; String response = JOptionPane.showInputDialog(null,"How many problems would you like to solve?");&nbsp; &nbsp; &nbsp; &nbsp; int noOfTimes = Integer.parseInt(response); // answer from question&nbsp; &nbsp; &nbsp; &nbsp; String result= null;&nbsp; &nbsp; &nbsp; &nbsp; for (int counter = 0; counter < noOfTimes; counter++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Random obj = new Random();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int num1 = obj.nextInt(10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int num2 = obj.nextInt(10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int rand = num1 + num2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int answer = Integer.parseInt(JOptionPane.showInputDialog(num1 + "+" +num2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (answer == rand){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result= "Correct";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result= "Incorrect";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, result);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java