(1) 请用java写一个代码批量生成产生习题并用文件存储; (2) 请用java写一个代码生成100以内加法/减法的算式基; (3) 请用java写一个代码批改练习并存储。
时间: 2024-02-29 15:52:02 浏览: 122
生成加减法的java代码
好的,我会分别为您提供Java代码。首先是批量生成习题并用文件存储的代码:
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class MathExercisesGenerator {
public static void main(String[] args) {
int numOfExercises = 50; // 题目数量
int maxNum = 100; // 最大数值
String fileName = "math_exercises.txt"; // 存储文件名
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
Random random = new Random();
for (int i = 0; i < numOfExercises; i++) {
int num1 = random.nextInt(maxNum) + 1;
int num2 = random.nextInt(maxNum) + 1;
int operator = random.nextInt(2); // 0代表加法,1代表减法
String operatorStr = operator == 0 ? "+" : "-";
int result = operator == 0 ? num1 + num2 : num1 - num2;
String exercise = num1 + " " + operatorStr + " " + num2 + " = ";
writer.write(exercise + "\n");
}
writer.close();
System.out.println("Math exercises generated successfully!");
} catch (IOException e) {
System.out.println("Error occurred while generating math exercises.");
e.printStackTrace();
}
}
}
```
代码中通过设置`numOfExercises`和`maxNum`变量来控制生成的习题数量和数值的范围,可以根据需要进行修改。生成的习题会存储在一个名为`math_exercises.txt`的文件中。
接下来是生成100以内加法/减法的算式基的代码:
```java
import java.util.ArrayList;
import java.util.List;
public class MathProblemsGenerator {
public static void main(String[] args) {
int maxNum = 100; // 最大数值
List<String> problems = new ArrayList<>();
// 生成加法算式基
for (int i = 1; i <= maxNum; i++) {
for (int j = 1; j <= maxNum; j++) {
int result = i + j;
String problem = i + "+" + j + "=" + result;
problems.add(problem);
}
}
// 生成减法算式基
for (int i = 1; i <= maxNum; i++) {
for (int j = 1; j <= i; j++) {
int result = i - j;
String problem = i + "-" + j + "=" + result;
problems.add(problem);
}
}
System.out.println("Math problems generated successfully!");
// 打印生成的算式
for (String problem : problems) {
System.out.println(problem);
}
}
}
```
代码中通过设置`maxNum`变量来控制数值的范围,可以根据需要进行修改。生成的算式会存储在一个字符串列表中,并打印输出到控制台。
最后是批改练习并存储的代码:
```java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MathExercisesChecker {
public static void main(String[] args) {
String exercisesFile = "math_exercises.txt"; // 习题文件名
String answersFile = "math_answers.txt"; // 答案文件名
String resultFile = "math_result.txt"; // 批改结果文件名
try {
BufferedReader exercisesReader = new BufferedReader(new FileReader(exercisesFile));
BufferedReader answersReader = new BufferedReader(new FileReader(answersFile));
BufferedWriter resultWriter = new BufferedWriter(new FileWriter(resultFile));
List<String> exercises = new ArrayList<>();
List<String> answers = new ArrayList<>();
String line;
// 读取习题和答案
while ((line = exercisesReader.readLine()) != null) {
exercises.add(line.trim());
}
while ((line = answersReader.readLine()) != null) {
answers.add(line.trim());
}
exercisesReader.close();
answersReader.close();
// 批改习题
for (int i = 0; i < exercises.size(); i++) {
String exercise = exercises.get(i);
String answer = answers.get(i);
String result;
if (exercise.endsWith("+ ")) { // 加法
int num1 = Integer.parseInt(exercise.substring(0, exercise.indexOf("+")).trim());
int num2 = Integer.parseInt(answer.trim());
int correctResult = num1 + num2;
result = correctResult == Integer.parseInt(exercise.substring(exercise.indexOf("=") + 1, exercise.length() - 1).trim()) ? "Correct" : "Incorrect";
} else { // 减法
int num1 = Integer.parseInt(exercise.substring(0, exercise.indexOf("-")).trim());
int num2 = Integer.parseInt(answer.trim());
int correctResult = num1 - num2;
result = correctResult == Integer.parseInt(exercise.substring(exercise.indexOf("=") + 1, exercise.length() - 1).trim()) ? "Correct" : "Incorrect";
}
// 写入批改结果文件
resultWriter.write(exercise + answer + result + "\n");
}
resultWriter.close();
System.out.println("Math exercises checked successfully!");
} catch (IOException e) {
System.out.println("Error occurred while checking math exercises.");
e.printStackTrace();
}
}
}
```
代码中通过设置`exercisesFile`、`answersFile`和`resultFile`变量来指定习题文件、答案文件和批改结果文件的文件名,可以根据需要进行修改。批改结果会存储在名为`math_result.txt`的文件中。
阅读全文