作业详情
-
//20213992 lizhizhang
package com.company;
import java.util.Random;
public class ketang {
public static void main(String[] args) {
int numProblems = 10; // 生成题目的数量
int maxNumbers = 5; // 每个题目包含的最大数字数量
Random random = new Random();
for (int i = 0; i < numProblems; i++) {
int numCount = random.nextInt(maxNumbers - 2) + 3; // 生成3到maxNumbers之间的随机数字数量
int[] numbers = new int[numCount];
// 生成随机数字
for (int j = 0; j < numCount; j++) {
numbers[j] = random.nextInt(20); // 生成0到19的随机数
}
// 随机选择第一个操作数
int result = numbers[0];
for (int j = 1; j < numCount; j++) {
char operator;
// 随机选择是加法还是减法
if (random.nextBoolean()) {
operator = '+';
} else {
operator = '-';
}
// 根据操作符进行计算
if (operator == '+') {
result += numbers[j];
} else {
result -= numbers[j];
}
System.out.print(numbers[j]);
if (j < numCount - 1) {
System.out.print(" " + operator + " ");
} else {
System.out.print(" = " + result);
}
}
System.out.println();
}
}
}