采用java编写一个软件,100以内的口算题【软件构造大作业】
看了另一位大佬的帖子,自己搞了搞,比较菜,勿喷
链接在底部。
(1)启动项目后,访问http://localhost:8080/getCalculation,为展示方便,以生成10道计算题为例展示,主页如下:
(2)前端js验证要求运算结果位于[0,100],且必须全部答完,否则不能提交。
(3)作答提交
(4)提交后得到本次的错题集,并自动计算本次得分和错题数。
(5)错题已经被保存到文件中且不会被覆盖
(6)点击按钮会自动返回练习主页,可以选择继续练习
(7)编写完工具类后的单元测试,显示成功输出。
下面是代码,本人才疏学浅,大佬轻喷:
/**
* @author 86178
*/
@Configuration
public class CalculationUtil {
@Bean
public Problem getCalculation() {
List<Calculation> 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;
}
}
/**
* @author 86178
*/
@Controller
public class CalculationController {
@Autowired
CalculationUtil calculationUtil;
@GetMapping("/getCalculation")
public String getCalculation(Model model) {
List<Calculation> calculationList = calculationUtil.getCalculation().getCalculations();
model.addAttribute("calculationList", calculationList);
return "calculations";
}
@PostMapping("/getMistake")
public String getProblem(@RequestParam("value") Integer[] value, Model model) throws IOException {
List<Mistake> 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";
}
}
/**
* @author 86178
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Calculation {
/**
* @id 计算题编号
*/
private Integer id;
/**
* @question 题目
*/
private String question;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Mistake {
/**
* @id 原题编号
*/
private Integer id;
/**
* @mistake 错题题目
*/
private String mistake;
/**
* @result 正确结果
*/
private Integer result;
/**
* @answer 用户答案
*/
private Integer answer;
}
/**
* @author 86178
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Problem {
/**
* @result 正确结果
*/
private Integer[] result;
/**
* @answer 用户答案
*/
private Integer[] answer;
/**
* @calculations 题目信息
*/
private List<Calculation> calculations;
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>计算题</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<style>
.table th, .table td {
text-align: center;
vertical-align: middle!important;
}
</style>
<script language="JavaScript">
function check() {
if (document.getElementById("value").value=="") {
alert("请答完全部的题目之后再提交!!!");
return false;
}
}
</script>
</head>
<body>
<div style="width: 900px; margin: 0 auto;">
<h3 class="text-center">加减计算题</h3><br>
<form action="/getMistake" method="post" name="form" onsubmit="return check()">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>编号</th>
<th>题目</th>
<th>请输入答案</th>
</tr>
</thead>
<tbody>
<tr th:each="calculation : ${calculationList}">
<td th:text="${calculation.getId()}"></td>
<td th:text="${calculation.getQuestion()}"></td>
<td>
<input type="number" id="value" name="value" max="100" min="0">
</td>
</tr>
</tbody>
</table><br>
<div style="text-align: center">
<button type="submit" class="btn">提交</button>
</div>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>错题集</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<style>
.table th, .table td {
text-align: center;
vertical-align: middle!important;
}
</style>
</head>
<body>
<div style="width: 900px; margin: 0 auto;">
<h3>错题集</h3><br>
<h4>
您本次共有 <span th:text="${mistakes}"></span> 道错题
</h4>
<h4>
您本次的得分是:<span th:text="${score}"></span>
</h4>
<h4>
您本次的错题已保存到【mistake.txt】文件中
</h4>
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>原题编号</th>
<th>题目</th>
<th>正确答案</th>
<th>您的答案</th>
</tr>
</thead>
<tbody>
<tr th:each="mistake : ${mistakeList}">
<td th:text="${mistake.getId()}"></td>
<td th:text="${mistake.getMistake()}"></td>
<td th:text="${mistake.getResult()}"></td>
<td th:text="${mistake.getAnswer()}"></td>
</tr>
</tbody>
</table>
<div style="text-align: center">
<button type="submit" id="btn" onclick="javascript:location.href='/getCalculation'">没有掌握?点我继续练习</button>
</div>
</div>
</body>
</html>
参考帖子: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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端