10.16每日总结
今天完成了软件构造的作业,代码如下:
package calculation; import java.util.Random; public class Equation { private int theFirstNumber; private int theSecondNumber; private String symbol; public Equation(int maxNumber, boolean allowMultiplicationDivision) { Random random = new Random(); this.theFirstNumber = random.nextInt(maxNumber - 9) + 10; this.theSecondNumber = random.nextInt(maxNumber - 9) + 10; if (allowMultiplicationDivision) { int randomOperator = random.nextInt(4); switch (randomOperator) { case 0: this.symbol = "+"; break; case 1: this.symbol = "-"; break; case 2: this.symbol = "*"; break; case 3: this.symbol = "/"; break; } } else { int randomOperator = random.nextInt(2); switch (randomOperator) { case 0: this.symbol = "+"; break; case 1: this.symbol = "-"; break; } } } public int getTheFirstNumber() { return theFirstNumber; } public void setTheFirstNumber(int theFirstNumber) { this.theFirstNumber = theFirstNumber; } public int getTheSecondNumber() { return theSecondNumber; } public void setTheSecondNumber(int theSecondNumber) { this.theSecondNumber = theSecondNumber; } public String getSymbol() { return symbol; } public void setSymbol(String symbol) { this.symbol = symbol; } @Override public String toString() { return theFirstNumber + " " + symbol + " " + theSecondNumber +" =" + " " ; } }
package calculation; import java.util.Scanner; import java.util.HashSet; public class Compute { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 获取用户输入,确定是否包括乘除法 System.out.print("是否包括乘除法(0代表没有仅有加减法,1代表有加减法也有乘除法): "); int allowMultiplicationDivision = scanner.nextInt(); // 获取用户输入,确定计算数的范围最大值 System.out.print("请输入计算数的范围最大值: "); int maxNumber = scanner.nextInt(); // 获取用户输入,确定出题的个数 System.out.print("请输入出题的个数(最多为50个): "); int numQuestions = scanner.nextInt(); // 检查出题的个数是否超过上限 if (numQuestions > 50) { System。out.println("出题的个数不能超过50。"); return; } // 创建 Equation 对象的数组,用于存储生成的题目 Equation[] equations = new Equation[numQuestions]; HashSet<String> uniqueEquations = new HashSet<>(); // 用于存储唯一的题目字符串 int generatedQuestions = 0; // 生成题目并确保它们不重复 while (generatedQuestions <》 numQuestions) { Equation newEquation = new Equation(maxNumber, allowMultiplicationDivision == 1); String equationString = newEquation.toString(); if (!uniqueEquations.contains(equationString)) { equations[generatedQuestions] = newEquation; uniqueEquations.add(equationString); generatedQuestions++; } } // 输出生成的题目 System.out.println("生成的题目如下:"); for (int i = 0; i < numQuestionns; i++) { System.out.print(equations[i]); if ((i + 1) % 4 == 0) { System.out.println(); } else { System.out.print("\t"); } } System.out.println(" "); System.out.println("现在可以开始做题了。"); } }
项目结构: