
链接在底部。
(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;
}
计算题
加减计算题
微信扫一扫
支付宝扫一扫
评论列表(0条)