四则运算2.0失败版本
server.port=8080
spring.datasource.url=jdbc:h2:mem:tested
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
package com.example.mathquiz;
// 替换为你的包名
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
import java.util.Random;
@Controller
public class MathProblemController {
@Autowired
private MathProblemRepository problemRepository;
@GetMapping("/")
public String index(Model model) {
// 生成随机题目并存储到数据库
for (int i = 0; i < 5; i++) {
String expression = generateRandomExpression();
int answer = calculateExpression(expression);
MathProblem problem = new MathProblem();
problem.setExpression(expression);
problem.setAnswer(answer);
problemRepository.save(problem);
}
// 查询数据库中的题目
List<MathProblem> problems = problemRepository.findAll();
model.addAttribute("problems", problems);
return "index";
}
// 生成随机四则运算表达式
private String generateRandomExpression() {
String[] operators = {"+", "-", "*", "/"};
Random random = new Random();
int operand1 = random.nextInt(10); // 生成随机整数作为第一个操作数
int operand2 = random.nextInt(10); // 生成随机整数作为第二个操作数
String operator = operators[random.nextInt(operators.length)]; // 随机选择一个运算符
// 构建表达式字符串
return operand1 + " " + operator + " " + operand2;
}
// 计算四则运算表达式的答案
private int calculateExpression(String expression) {
String[] parts = expression.split(" ");
int operand1 = Integer.parseInt(parts[0]);
String operator = parts[1];
int operand2 = Integer.parseInt(parts[2]);
switch (operator) {
case "+":
return operand1 + operand2;
case "-":
return operand1 - operand2;
case "*":
return operand1 * operand2;
case "/":
if (operand2 != 0) {
return operand1 / operand2;
} else {
// 处理除零错误
return 0;
}
default:
// 处理无效运算符
return 0;
}
}
}
package com.example.mathquiz;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MathProblemRepository extends JpaRepository<MathProblem, Long> {
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>在线答题</title>
</head>
<body>
<h1>在线答题</h1>
<table>
<tr>
<th>题目</th>
<th>答案</th>
</tr>
<tr th:each="problem : ${problems}">
<td th:text="${problem.expression}"></td>
<td th:text="${problem.answer}"></td>
</tr>
</table>
</body>
</html>
package com.example.mathquiz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EntityScan(basePackages = "com.example.mathquiz")
@EnableJpaRepositories(basePackages = "com.example.mathquiz")
public class MathQuizApplication {
public static void main(String[] args) {
SpringApplication.run(MathQuizApplication.class, args);
}
}
package com.example.mathquiz;标签:return,String,int,2023.10,20,org,import,expression From: https://www.cnblogs.com/ztydebeishanglaojia/p/17781374.html
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class MathProblem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String expression;
private int answer;
// Getter and setter for expression
public String getExpression() {
return expression;
}
public void setExpression(String expression) {
this.expression = expression;
}
// Getter and setter for answer
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
}