用Java语言做:1、加法、减法、加减混合运算习题生成50道,每行五个算式;2、加法运算结果要在100以内;减法运算结果要大于0小于100;混合算式则是上述两条均满足;3、将生成的加法、减法、加减混合算法分别生成有结果的与无结果的csv文件并存入;4、写一个批改程序,分别读取无结果加减与混合算式的csv文件显示在控制台,可以在算式后面填写答案,再将答案与相应的有结果csv文件比较,显示错误个数以及错正却个数。5、csv文件命名为“”____习题文件_50道_01.csv“,例如加法则是"加法习题文件_50道_01.csv",无结果则是"加法习题文件_50道_02.csv",并且两者存放的算式完全一致,区别仅仅在于有无结果;7、生成相应的main方法。最后得出实验总结
时间: 2024-02-28 22:57:19 浏览: 200
以下是Java代码实现:
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class MathHomework {
private static final int NUM_OF_QUESTIONS = 50;
// 生成加法题目
private static String generateAdditionQuestions() {
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < NUM_OF_QUESTIONS; i++) {
int a = random.nextInt(100);
int b = random.nextInt(100 - a);
sb.append(a + " + " + b + " = ,");
}
return sb.toString();
}
// 生成减法题目
private static String generateSubtractionQuestions() {
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < NUM_OF_QUESTIONS; i++) {
int a = random.nextInt(100);
int b = random.nextInt(a);
sb.append(a + " - " + b + " = ,");
}
return sb.toString();
}
// 生成加减混合题目
private static String generateMixedQuestions() {
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < NUM_OF_QUESTIONS; i++) {
if (random.nextBoolean()) { // 生成加法
int a = random.nextInt(100);
int b = random.nextInt(100 - a);
sb.append(a + " + " + b + " = ,");
} else { // 生成减法
int a = random.nextInt(100);
int b = random.nextInt(a);
sb.append(a + " - " + b + " = ,");
}
}
return sb.toString();
}
// 生成有结果的csv文件
private static void generateResultCsvFile(String questions, String fileName) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
String[] questionArray = questions.split(",");
int count = 0;
for (String question : questionArray) {
bw.write(question);
bw.write(count % 5 == 4 ? "\n" : ",");
count++;
}
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
// 生成无结果的csv文件
private static void generateNoResultCsvFile(String questions, String fileName) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
String[] questionArray = questions.split(",");
for (String question : questionArray) {
bw.write(question.substring(0, question.lastIndexOf("=") + 2));
bw.write("\n");
}
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
// 批改程序
private static void checkAnswers(String noResultFileName, String resultFileName) {
try (Scanner scanner = new Scanner(System.in)) {
String[] noResultArray = Utils.readCsvFile(noResultFileName);
String[] resultArray = Utils.readCsvFile(resultFileName);
int errorCount = 0; // 错误个数
int correctErrorCount = 0; // 错正却个数
for (int i = 0; i < noResultArray.length; i++) {
System.out.print(noResultArray[i]);
String answer = scanner.nextLine();
// 比较答案
if (!answer.equals(resultArray[i].substring(resultArray[i].lastIndexOf("=") + 2))) {
errorCount++;
if (Integer.parseInt(answer) == Integer.parseInt(resultArray[i].substring(resultArray[i].lastIndexOf("=") + 2))) {
correctErrorCount++;
}
}
}
System.out.println("错误个数:" + errorCount);
System.out.println("错正却个数:" + correctErrorCount);
}
}
public static void main(String[] args) {
String additionQuestions = generateAdditionQuestions();
String subtractionQuestions = generateSubtractionQuestions();
String mixedQuestions = generateMixedQuestions();
generateResultCsvFile(additionQuestions, "加法习题文件_50道_01.csv");
generateNoResultCsvFile(additionQuestions, "加法习题文件_50道_02.csv");
generateResultCsvFile(subtractionQuestions, "减法习题文件_50道_01.csv");
generateNoResultCsvFile(subtractionQuestions, "减法习题文件_50道_02.csv");
generateResultCsvFile(mixedQuestions, "混合算式习题文件_50道_01.csv");
generateNoResultCsvFile(mixedQuestions, "混合算式习题文件_50道_02.csv");
checkAnswers("加法习题文件_50道_02.csv", "加法习题文件_50道_01.csv");
checkAnswers("减法习题文件_50道_02.csv", "减法习题文件_50道_01.csv");
checkAnswers("混合算式习题文件_50道_02.csv", "混合算式习题文件_50道_01.csv");
}
}
```
实验总结:本次实验主要学习了Java中的文件读写操作、随机数生成、字符串操作、批处理程序等知识点。在实现过程中,需要注意算式范围的限制、文件名的命名规范以及程序逻辑的正确性,以保证程序的可读性和正确性。通过本次实验,我对Java语言的基础知识有了更深入的理解,并且加强了对Java语言的编程能力和实践能力。
阅读全文