链接在底部。
(1)启动项目后,访问http://localhost:8080/getCalculation,为展示方便,以生成10道计算题为例展示,主页如下:
(2)前端js验证要求运算结果位于[0,100],且必须全部答完,否则不能提交。
(3)作答提交
(4)提交后得到本次的错题集,并自动计算本次得分和错题数。
(5)错题已经被保存到文件中且不会被覆盖
(6)点击按钮会自动返回练习主页,可以选择继续练习
(7)编写完工具类后的单元测试,显示成功输出。
下面是代码,本人才疏学浅,大佬轻喷:
@Configuration
public class CalculationUtil {
@Bean
public Problem getCalculation() {
List calculationList = new ArrayList<>();
Problem problem = new Problem();
Integer[] result = new Integer[10];
int m = 0, n = 0, value = 0;
String op = "+";
Random random = new Random();
for (int i = 0; i < 10; i++) {
int o = random.nextInt(2);
if (o == 0) {
op = "+";
do{
m = random.nextInt(101);
n = random.nextInt(101);
value = m + n;
}while (value > 100);
} else {
op = "-";
do {
m = random.nextInt(101);
n = random.nextInt(101);
value = m - n;
}while (value < 0);
}
Calculation calculation = new Calculation();
calculation.setId(i+1);
calculation.setQuestion(m + op + n + " = ");
calculationList.add(calculation);
result[i] = value;
}
problem.setResult(result);
problem.setCalculations(calculationList);
return problem;
}
}
@Controller
public class CalculationController {
@Autowired
CalculationUtil calculationUtil;
@GetMapping("/getCalculation")
public String getCalculation(Model model) {
List calculationList = calculationUtil.getCalculation().getCalculations();
model.addAttribute("calculationList", calculationList);
return "calculations";
}
@PostMapping("/getMistake")
public String getProblem(@RequestParam("value") Integer[] value, Model model) throws IOException {
List mistakeList = new ArrayList<>();
Integer[] result = calculationUtil.getCalculation().getResult();
Integer[] answer = value;
Integer score = 100;
Integer mistakes = 0;
for (int i = 0; i < calculationUtil.getCalculation().getCalculations().size(); i++) {
if (!result[i].equals(answer[i])) {
score-=10;
mistakes+=1;
Mistake mistake = new Mistake();
mistake.setId(calculationUtil.getCalculation().getCalculations().get(i).getId());
mistake.setMistake(calculationUtil.getCalculation().getCalculations().get(i).getQuestion());
mistake.setResult(result[i]);
mistake.setAnswer(answer[i]);
mistakeList.add(mistake);
}
}
BufferedWriter bw = new BufferedWriter(new FileWriter("mistake.txt", true));
for (Mistake mistake : mistakeList) {
bw.write("【原题编号:" + mistake.getId() + "】【错题题目:" + mistake.getMistake() +
"】【正确答案:" + mistake.getResult() + "】【您的答案:" + mistake.getAnswer() + "】");
bw.newline();
bw.flush();
}
model.addAttribute("mistakes", mistakes);
model.addAttribute("score", score);
model.addAttribute("mistakeList", mistakeList);
return "mistake";
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Calculation {
private Integer id;
private String question;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Mistake {
private Integer id;
private String mistake;
private Integer result;
private Integer answer;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Problem {
private Integer[] result;
private Integer[] answer;
private List calculations;
}
计算题
加减计算题
错题集
错题集
您本次共有 道错题
您本次的得分是:
您本次的错题已保存到【mistake.txt】文件中
原题编号
题目
正确答案
您的答案
参考帖子:https://blog.csdn.net/qq_51863442/article/details/121022246?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163739713216780271570821%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=163739713216780271570821&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-1-121022246.first_rank_v2_pc_rank_v29&utm_term=%E9%87%87%E7%94%A8java%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E8%BD%AF%E4%BB%B6%EF%BC%8C100%E4%BB%A5%E5%86%85%E7%9A%84%E5%8F%A3%E7%AE%97%E9%A2%98&spm=1018.2226.3001.4187



