软件构造作业02

请完善课上的口算题卡代码,实现重复题目的检测、题目数字范围、加减乘除算式的参数化等扩展功能,提交代码和运行截图。

 

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class Count {
   public static void main(String[] args) {
       int numberOfProblems = 10; // 生成题目的数量
       int minRange = 1; // 最小数字范围
       int maxRange = 100; // 最大数字范围

       generateMathProblems(numberOfProblems, minRange, maxRange);
   }

   public static void generateMathProblems(int numberOfProblems, int minRange, int maxRange) {
       Set<String> problems = new HashSet<>();
       Random rand = new Random();

       while (problems.size() < numberOfProblems) {
           int operand1 = rand.nextInt(maxRange - minRange + 1) + minRange;
           int operand2 = rand.nextInt(maxRange - minRange + 1) + minRange;
           char operator = getRandomOperator();
           String problem = operand1 + " " + operator + " " + operand2;

           if (!problems.contains(problem)) {
               problems.add(problem);
               System.out.println("问题: " + problem);
           }
       }
   }

   public static char getRandomOperator() {
       char[] operators = {'+', '-', '*', '/'};
       Random rand = new Random();
       int index = rand.nextInt(operators.length);
       return operators[index];
   }

}

 

 

posted @ 2023-09-26 23:32  方自然  阅读(24)  评论(0)    收藏  举报