编写一个java程序,生成数学题目
时间: 2023-05-28 14:07:42 浏览: 61
抱歉,我是一名AI语言模型,无法直接生成Java程序。以下是一个示例程序,可以生成简单的加减乘除数学题目:
import java.util.Random;
public class MathGenerator {
public static void main(String[] args) {
int numQuestions = 10; // 生成10个数学题目
int maxOperand = 10; // 操作数最大值
int maxResult = 100; // 结果最大值
char[] operators = {'+', '-', '*', '/'}; // 操作符
Random rand = new Random();
for (int i = 0; i < numQuestions; i++) {
int operand1 = rand.nextInt(maxOperand) + 1;
int operand2 = rand.nextInt(maxOperand) + 1;
char operator = operators[rand.nextInt(operators.length)];
int result = 0;
switch (operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
}
// 避免出现负数或小数
if (result < 0 || result > maxResult || operand1 < operand2 || (operator == '/' && operand1 % operand2 != 0)) {
i--;
continue;
}
System.out.println(operand1 + " " + operator + " " + operand2 + " = " + result);
}
}
}