import java.util.Random;
public class MathOperationGenerator {
public static void main(String[] args) {
int numberOfQuestions = 10; // 指定生成题目的数量
generateMathQuestions(numberOfQuestions);
}
public static void generateMathQuestions(int numQuestions) {
Random random = new Random();
for (int i = 0; i < numQuestions; i++) {
int operand1 = random.nextInt(100); // 随机生成第一个操作数(0-99之间的整数)
int operand2 = random.nextInt(100); // 随机生成第二个操作数(0-99之间的整数)
int operator = random.nextInt(2); // 随机选择运算符:0代表加法,1代表减法
String operatorSymbol;
int answer;
if (operator == 0) {
operatorSymbol = "+";
answer = operand1 + operand2;
} else {
operatorSymbol = "-";
answer = operand1 - operand2;
}
System.out.println("Question " + (i + 1) + ": " + operand1 + " " + operatorSymbol + " " + operand2 + " = " + answer);
}
}
}
标签:截图,operand1,operand2,int,random,operatorSymbol,课上,answer,加减法 From: https://www.cnblogs.com/DREAM2021/p/17770081.html