今日代码:200行
今日时间:4小时
学习内容:今天做了软件构造的作业
小学数学题的编程
MathPaper.java
package com.stdu.www;
import java.util.ArrayList;
import java.util.List;
public class MathPaper {
private List<MathQuestion> questions;
public MathPaper(int minValue, int maxValue, int totalQuestions) {
questions = new ArrayList<>();
for(int i = 0; i < totalQuestions; i++) {
questions.add(new MathQuestion(minValue, maxValue));
}
}
public String getQuestions() {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < questions.size(); i++) {
sb.append(i+1).append(". ").append(questions.get(i).getQuestion()).append("\n");
}
return sb.toString();
}
public String checkAnswers(int[] answers) {
StringBuilder sb = new StringBuilder();
int correctCount = 0;
for(int i = 0; i < questions.size(); i++) {
if(questions.get(i).checkAnswer(answers[i])) {
sb.append(i+1).append(". 回答正确\n");
correctCount++;
} else {
sb.append(i+1).append(". 回答错误,正确答案为:").append(questions.get(i).toString()).append("\n");
}
}
sb.append("共").append(questions.size()).append("题,答对").append(correctCount).append("题,正确率为").append((double)correctCount/questions.size()*100).append("%\n");
return sb.toString();
}
}
MathQuestion.java
package com.stdu.www;
import java.util.Random;
public class MathQuestion {
private int num1;
private int num2;
private char operator;
private int answer;
public MathQuestion(int minValue, int maxValue) {
Random random = new Random();
num1 = random.nextInt(maxValue - minValue + 1) + minValue;
num2 = random.nextInt(maxValue - minValue + 1) + minValue;
operator = generateOperator(random);
answer = calculateAnswer();
}
private char generateOperator(Random random) {
switch (random.nextInt(4)) {
case 0:
return '+';
case 1:
return '-';
case 2:
return '*';
case 3:
return '/';
default:
return '+';
}
}
private int calculateAnswer() {
switch (operator) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if(num2 == 0 || num1 % num2 != 0) {
throw new ArithmeticException("除数不能为0,或无法整除");
} else {
return num1 / num2;
}
default:
throw new IllegalArgumentException("不支持该运算符:" + operator);
}
}
public String getQuestion() {
return num1 + " " + operator + " " + num2 + " = ";
}
public boolean checkAnswer(int answer) {
return this.answer == answer;
}
@Override
public String toString() {
return getQuestion() + answer;
}
}
MathTest.java
package com.stdu.www;
import java.util.Scanner;
public class MathTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入最小值:");
int minValue = scanner.nextInt();
System.out.print("请输入最大值:");
int maxValue = scanner.nextInt();
System.out.print("请输入题目数量:");
int totalQuestions = scanner.nextInt();
MathPaper paper = new MathPaper(minValue, maxValue, totalQuestions);
System.out.println(paper.getQuestions());
int[] answers = new int[totalQuestions];
for(int i = 0; i < totalQuestions; i++) {
System.out.print("请输入第" + (i+1) + "题的答案:");
answers[i] = scanner.nextInt();
}
System.out.println(paper.checkAnswers(answers));
}
背四级单词,看了一些css知识
}
标签:return,int,10.18,questions,new,public,append From: https://www.cnblogs.com/zhaoshengfu/p/17773212.html