简单的Java数学问题生成器运行和终止

问题描述 投票:0回答:2

我的程序应该问用户一个乘法问题。如果用户回答正确,它将显示“很好!这是另一个问题:[插入问题]”(它永远不会停止这样做)。如果用户回答不正确,程序将显示“不是。请重试:[相同问题]”,直到用户正确回答为止。它询问第一个问题,用户正确回答,但没有显示应显示的消息。当用户第二次获得正确答案时,它将显示该消息。用户第三次获得正确答案后,它将终止。值得庆幸的是,用户会无限期地重复相同的问题(直到用户正确为止,程序不会从相同的问题继续进行)。这是代码:

import java.security.SecureRandom;
import java.util.Scanner;

public class MethodMath {
    static int num1; // Holds actual secure random number
    static int num2; // Holds another actual secure random number
    static SecureRandom number1; // Becomes a new instance of secureRandom
    static SecureRandom number2; // Becomes another new instance of secureRandom
    static int answer; // Becomes user input
    private static Scanner input = new Scanner(System.in);// Creates new scanner

    MethodMath() {  } // Constructor

    public void isFalse() {
        while (num1 * num2 != answer) { // Asks same question until user gets it right
            System.out.print("Not quite. Try again: What is " + num1 + " times " + num2 + "?: ");
            answer = input.nextInt();
        }
    }

    public boolean numGen() { // Generates multiplication problems ()
        number1 = new SecureRandom();
        num1 = number1.nextInt(10);

        number2 = new SecureRandom();
        num2 = number2.nextInt(10);

        System.out.print("What is " + num1 + " times " + num2 + "?: ");
        answer = input.nextInt();
        this.isFalse();
        return true;
    }

    public static void main(String[] args) {
        MethodMath mm = new MethodMath();

        // Computer assisted instruction program
        mm.numGen(); // First problem
        if (mm.numGen()) { // If numGen returns true
            System.out.println("Great job! Here is another problem: ");
            mm.numGen();
        }
    }
}

为什么在3个正确答案后会终止?

java math
2个回答
0
投票
public static void main(String[] args) { MethodMath mm = new MethodMath(); while(mm.numGen()) { System.out.println("Great job! Here is another problem: "); } }

0
投票
if (mm.numGen()) {// If numGen returns true System.out.println("Great job! Here is another problem: "); mm.numGen();}

至:

while (mm.numGen()) {// If numGen returns true
        System.out.println("Great job! Here is another problem: ");}

说明:

虽然括号中的条件为true(在这种情况下,您的numGen返回true),代码将继续执行
© www.soinside.com 2019 - 2024. All rights reserved.