loops - 对于循环数学测验生成器,循环不重复

标签 loops for-loop nested

所以我有一个作业,其中我应该用随机数生成一个包含五个问题的数学测验,并且我应该在 for 中为具有某些数学属性的 0-2 分配数学运算符。环形。我已经束手无策了;我不明白为什么它不循环重复。

import java.util.Scanner;
import java.util.Random;
public class MathQuizS2 {

    public static void main(String args[]){

        int first, second, sum;
        int attempt, diff, mult;
        int op;
        int total;
        final int correctMax = 5;
        int correct = 0;
        int incorrect = 0;
        double grade;

        Scanner scan = new Scanner(System.in);
        Random generator = new Random();

        op = generator.nextInt(3);
        first = generator.nextInt(11);

        second = generator.nextInt(11);

        sum = first + second;
        diff = first - second;
        mult = first * second;

        {
            for (total=0; total<=correctMax; total++)
            {
                if (op == 0)
                    System.out.println(" What is the sum of " + first + " + " + second + " ? ");

                attempt = scan.nextInt();

                if (sum == attempt) {
                    System.out.println("You are correct");
                    correct++;
                }
                else {
                    System.out.println("Incorrect the sum is " + sum);
                    incorrect++;
                }

                if (op == 1)
                    System.out.println(" What is the difference between " + first + "-" + second + " ? ");

                attempt = scan.nextInt();

                if (diff == attempt) {
                    System.out.println("You are correct");
                    correct++;
                }
                else {
                    System.out.println("Incorrect the difference is " + diff);
                    incorrect++;
                }

                if (op == 2)
                    System.out.println(" What is the product of " + first + " * " + second + " ? ");

                attempt = scan.nextInt();

                if (mult == attempt) {
                    System.out.println("You are correct");
                    correct++;
                }
                else {
                    System.out.println("Incorrect the product is " + mult);
                    incorrect++;
                }
                total = 5;
            }
        }
        grade = (double) correct/5 * 100;
        System.out.println(" You got " + correct + " correct and " + incorrect + " incorrect of 5 questions, your grade is " + grade + "%");
    }
}

最佳答案

if (op... 构造中,只有下一条语句(在本例中直到分号,即下一行),即 println 语句是受条件影响,而不是下面的 attempt = scan.nextInt(); 等行。它们是无条件执行的。根据源代码的原始缩进,这不是本意。

            if (op == 0)
                System.out.println(" What is the sum of " + first + " + " + second + " ? ");

            attempt = scan.nextInt();

            if (sum == attempt) {
                System.out.println("You are correct");
                correct++;
            }
            else {
                System.out.println("Incorrect the sum is " + sum);
                incorrect++;
            }

如果您希望所有行都受到这些条件的影响,请在 if 结构中使用 {}:

            if (op == 0) {
                System.out.println(" What is the sum of " + first + " + " + second + " ? ");

                attempt = scan.nextInt();

                if (sum == attempt) {
                    System.out.println("You are correct");
                    correct++;
                }
                else {
                    System.out.println("Incorrect the sum is " + sum);
                    incorrect++;
                }
            }

这是大多数编码标准强制要求 {} 的原因之一。

关于loops - 对于循环数学测验生成器,循环不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28975986/

相关文章:

for-loop - for循环在Swift中更改按钮标题

javascript - 使用javascript遍历phantomjs中的网址数组

python - 在类中嵌套自定义异常类? (Python)

sorting - ElasticSearch:对复杂查询进行排序

linux - 如何将 wget 输出重命名为文件中的名称(bash)

python - 绕过循环AttributeError : 'NoneType' object has no attribute 'findAll'

java - for 循环中的 index*int 如何以零结束?

python - 由于元组错误,无法访问多个嵌套字典中的字典

loops - 使用HtmlAgilityPack选择SelectedNodes中的每个节点

python - 无法在 trie 遍历中重置字典指针