【问题标题】:Writing program to help elementary students learn multiplication帮助小学生学习乘法的写作程序
【发布时间】:2019-10-17 16:31:57
【问题描述】:

编写一个程序,帮助小学生学习乘法。使用 SecureRandom 对象生成两个正的一位整数(您需要查看如何执行此操作)。然后程序应该提示用户一个问题,例如

6乘以7等于多少?然后学生输入答案。接下来,程序会检查学生的答案。如果正确,则显示消息“非常好!”并问另一个乘法问题。如果答案错误,显示消息“No. Please try again.>again.”。并让学生反复尝试同一个问题,直到学生最终答对为止。

应该使用单独的方法来生成每个新问题。此方法应在应用程序开始执行时以及每次用户正确回答问题时调用一次。

我的问题是你必须做一个 if else 语句 == my public static mathQuestion 然后让它输出吗?制作 SecureRandom 后,我不知道该怎么做。我还是 Java 新手。

在多次错过问题后,我尝试执行 if-else 语句,但它已在方法中完成。

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

public class h_p1 {
    static SecureRandom rand = new SecureRandom();
    static Scanner sc = new Scanner (System.in);
 public static int mathQuestion() {
    int n1 = rand.nextint(9) + 1;
    int n2 = rand.nextint(9) + 1;

    System.out.print("What is" + n1 + "x" + n2"?");
    return r1 * r2;
}


}

}

【问题讨论】:

  • 一个新的乘法问题需要问多少次?如果计数是固定的,则使用for 循环,否则使用do-while 循环。在循环内部生成一组新数字,然后使用另一个do-while 循环来检查答案是否正确。

标签: java methods secure-random


【解决方案1】:

您需要执行以下操作:

  1. 提示答案
  2. 使用 if 语句检查答案。
  3. 如果答案不正确,请再次提示。
  4. 如果正确,请生成另一个问题。

在这种情况下,您需要使用循环。它可以承担 不同的设计,但您需要一种用于重新提示,另一种用于 新问题。

Imo,进行重新提示的最佳方法是使用带有可设置布尔值的 while 语句。如果他们得到正确的答案,则将布尔值设置为 false 否则,在 true 时继续提示。如果您想限制猜测次数,也可以使用 for 循环。

【讨论】:

    【解决方案2】:
    public class Quiz {
    
        public static void main(String[] args) {
    
            generateRandomNumbers();
    
        }
    
        public static void generateRandomNumbers() {
            SecureRandom rand = new SecureRandom();
            Scanner sc = new Scanner (System.in);
    
            int n1 = rand.nextInt(9) + 1;
            int n2 = rand.nextInt(9) + 1;
    
            generateQuestion(n1,n2);
        }
    
        public static void generateQuestion(int n1, int n2) {
    
            Scanner sc = new Scanner (System.in);
    
            System.out.print("What is " + n1 + " x " + n2+ " ?");
    
            int typedAnswer = sc.nextInt();
    
            if(typedAnswer == (n1*n2)) {
                System.out.println("Correct Answer");
                generateRandomNumbers();
            }else {
                System.out.println("Wrong Answer");
                generateQuestion(n1,n2);
            }
    
        }
    
    }
    

    【讨论】:

    • 如果答案是正确的,它将创建新的随机数字,并创建一个新问题。如果答案错误,则需要使用相同的随机数。因此,我将其传入参数以生成相同的问题。
    • 你总是写这样的递归代码吗?这段代码的空间复杂度是多少?
    • 这是我想到这个问题的答案。如果您知道任何为此优化的代码,请将其添加为答案。所以我也可以学。
    【解决方案3】:

    我能想到的这个问题的最简单答案:

    public static void main(String[] args) {
    
        Scanner scanner = new Scanner(System.in);
        do {
            SecureRandom secureRandom = new SecureRandom();
            int numbOne = secureRandom.nextInt(9) + 1;
            int numbTwo = secureRandom.nextInt(9) + 1;
            int prod = numbOne * numbTwo;
            int response;
            do {
                System.out.println(MessageFormat.format("What is the product of {0} and {1}", numbOne, numbTwo));
                response = scanner.nextInt();
                if (response != prod) {
                    System.out.println("Incorrect answer! Try again");
                }
            } while (response != prod);
            System.out.println("Correct answer");
            System.out.println("Do you want to practice with another question (Y/N)?");
        } while (scanner.next().equalsIgnoreCase("Y"));
    }
    

    它使用 2 个do-while 循环。外层循环根据用户的选择控制应问问题的次数,内层循环检查用户给出的答案的正确性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 2023-01-02
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多