写在前面
1、基于2022级软件工程实验指导书
2、代码仅提供参考
3、如果代码不满足你的要求,请寻求其他的途径
运行环境
window11家庭版
IntelliJ IDEA 2023.2.2
jdk17.0.6
实验要求
构造任务:生成50道100以内加法/减法算式的习题,并输出。
(1)生成50道100以内加法算式的习题,显示2套题;
(2)生成50道100以内减法算式的习题,显示2套题;
(3)对(1),每个算式还需要答案,显示2套题;
(4)对(2),每个算式还需要答案,显示2套题;
(5)加法算式的和不能超过100,如何确定,显示2套题;
(6)减法算式的差不能小于0,如何确定,显示2套题;
(7)每行整齐地多4~6个算式,不必频繁地下拉屏幕看算式,加法习题、减法习各2套;
(8)生成50道100以内加减法算式的加减法混合习题2套,满足(3)~(7),题中不能有相同的算式,如何确定,显示2套题。
源代码
Main.java
import java.util.*;
public class Main {
public static void main(String[] args) {
int set = 2;//习题套数
int number = 50;//习题数量
System.out.println("构造要求1结果:");
addition(set, number);
System.out.println("构造要求2结果:");
subtraction(set, number);
System.out.println("构造要求3结果:");
addition_answer(set, number);
System.out.println("构造要求4结果:");
subtraction_answer(set, number);
System.out.println("构造要求5结果:");
addition_answer_limit(set, number);
System.out.println("构造要求6结果:");
subtraction_answer_limit(set, number);
System.out.println("构造要求7结果:");
addition_answer_limit_tidiness(set, number);
subtraction_answer_limit_tidiness(set, number);
System.out.println("构造要求8结果:");
mix_answer_limit_tidiness_remove(set, number);
}
//构造要求1:生成50道100以内加法算式的习题,显示两套题
public static void addition(int set, int number) {
System.out.println("加法习题:");
for (int i = 0; i < set; i++) {
System.out.println("习题集 " + (i + 1) + ":");
for (int j = 0; j < number; j++) {
int num1 = new Random().nextInt(100);
int num2 = new Random().nextInt(100);
System.out.printf("%d+%d ", num1, num2);
}
System.out.println("\n");
}
}
//构造要求2:生成50道100以内减法算式的习题,显示两套题
public static void subtraction(int set, int number) {
System.out.println("减法习题:");
for (int i = 0; i < set; i++) {
System.out.println("习题集 " + (i + 1) + ":");
for (int j = 0; j < number; j++) {
int num1 = new Random().nextInt(100);
int num2 = new Random().nextInt(100);
System.out.printf("%d-%d ", num1, num2);
}
System.out.println("\n");
}
}
//构造要求3:除构造要求1外,每个算式还需要答案
public static void addition_answer(int set, int number) {
System.out.println("加法习题(有答案):");
for (int i = 0; i < set; i++) {
System.out.println("习题集 " + (i + 1) + ":");
for (int j = 0; j < number; j++) {
int num1 = new Random().nextInt(100);
int num2 = new Random().nextInt(100);
int answer = num1 + num2;
System.out.printf("%d+%d=%d ", num1, num2, answer);
}
System.out.println("\n");
}
}
//构造要求4:除构造要求2外,每个算式还需要答案
public static void subtraction_answer(int set, int number) {
System.out.println("减法习题(有答案):");
for (int i = 0; i < set; i++) {
System.out.println("习题集 " + (i + 1) + ":");
for (int j = 0; j < number; j++) {
int num1 = new Random().nextInt(100);
int num2 = new Random().nextInt(100);
int answer = num1 - num2;
System.out.printf("%d-%d=%d ", num1, num2, answer);
}
System.out.println("\n");
}
}
//构造要求5:除构造要求1、3外,加法算式的和不能超过100
public static void addition_answer_limit(int set, int number) {
System.out.println("加法习题(有答案)(和有限制):");
for (int i = 0; i < set; i++) {
System.out.println("习题集 " + (i + 1) + ":");
for (int j = 0; j < number; j++) {
int num1 = new Random().nextInt(101);
int num2 = new Random().nextInt(Math.min(101 - num1, 101));
int answer = num1 + num2;
System.out.printf("%d+%d=%d ", num1, num2, answer);
}
System.out.println("\n");
}
}
//构造要求6:除构造要求2、4外,减法算式的差不能小于0
public static void subtraction_answer_limit(int set, int number) {
System.out.println("减法习题(有答案)(差有限制):");
for (int i = 0; i < set; i++) {
System.out.println("习题集 " + (i + 1) + ":");
for (int j = 0; j < number; j++) {
int num1 = new Random().nextInt(101);
int num2 = new Random().nextInt(num1 + 1);
int answer = num1 - num2;
System.out.printf("%d-%d=%d ", num1, num2, answer);
}
System.out.println("\n");
}
}
//构造要求7:除构造要求1、3、5外,每行整齐地输出4~6个算式,不必频繁地下拉屏幕看算式
public static void addition_answer_limit_tidiness(int set, int number) {
System.out.println("加法习题(有答案)(和有限制)(格式对齐):");
for (int i = 0; i < set; i++) {
System.out.println("习题集 " + (i + 1) + ":");
for (int j = 0; j < number; j++) {
int num1 = new Random().nextInt(101);
int num2 = new Random().nextInt(Math.min(101 - num1, 101));
int answer = num1 + num2;
System.out.printf("%3d + %3d = %4d\t\t", num1, num2, answer);
if (j % 5 == 4)
System.out.println();
}
System.out.println();
}
}
//构造要求7:除构造要求2、4、6外,每行整齐地输出4~6个算式,不必频繁地下拉屏幕看算式
public static void subtraction_answer_limit_tidiness(int set, int number) {
System.out.println("减法习题(有答案)(差有限制)(格式对齐):");
for (int i = 0; i < set; i++) {
System.out.println("习题集 " + (i + 1) + ":");
for (int j = 0; j < number; j++) {
int num1 = new Random().nextInt(101);
int num2 = new Random().nextInt(num1 + 1);
int answer = num1 - num2;
System.out.printf("%3d - %3d =%4d\t\t", num1, num2, answer);
if (j % 5 == 4)
System.out.println();
}
System.out.println();
}
}
//构造要求8:除构造要求3、4、5、6、7外,生成50道100以内加减法算式的加减法混合习题两套,题中不能有相同的算式
public static void mix_answer_limit_tidiness_remove(int set, int number) {
System.out.println("加减混合习题(有答案)(和、差有限制)(格式对齐)(算式不相同):");
for (int i = 0; i < set; i++) {
System.out.println("习题集 " + (i + 1) + ":");
Set<String> expressions = new HashSet<>();
while (expressions.size() < number) {
Random random = new Random();
boolean isAddition = random.nextBoolean();
int num1, num2;
int answer;
if (isAddition) {
num1 = random.nextInt(101);
num2 = random.nextInt(101 - num1);
answer = num1 + num2;
} else {
num1 = random.nextInt(101);
num2 = random.nextInt(num1 + 1);
answer = num1 - num2;
}
String expression = String.format("%3d %s %3d = %4d", num1, isAddition ? "+" : "-", num2, answer);
expressions.add(expression);
}
int count = 0;
for (String expression : expressions) {
System.out.print(expression + "\t\t");
if (++count % 5 == 0)
System.out.println();
}
System.out.println();
}
}
}