首页 > 其他分享 >2023.10.20

2023.10.20

时间:2023-10-22 23:22:59浏览次数:37  
标签:return String int 2023.10 20 org import expression

四则运算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;


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;
}
}

标签:return,String,int,2023.10,20,org,import,expression
From: https://www.cnblogs.com/ztydebeishanglaojia/p/17781374.html

相关文章

  • 2023.10.17
    第13节:估算这一节强调了估算在软件开发中的重要性,并提供了一些建议来提高估算的准确性。1.通过学习估算并发展直觉,你可以展现出一种魔法般的能力,来确定任务的可行性。2.让估算更准确的单位值和描述方法是至关重要的,比如将时间估算从粗略的“大约6个月”改进为更精确的“130个......
  • 2023-2024-1 20231406 《计算机基础与程序设计》第4周学习总结
    2023-2024-120231406《计算机基础与程序设计》第4周学习总结作业信息这个作业属于哪个课程<班级的链接>(如[2023-2024-1-计算机基础与程序设计](https://edu.cnblogs.com/campus/besti/2023-2024-1-CFAP)这个作业要求在哪里<作业要求的链接>(如2023-2024-1计算机基础......
  • 2023-2024-1 学号20231315第四周学习总结
    学期:2023-2024-1学号:20231315《计算机基础与程序设计》第二周学习总结作业信息这个作业属于哪个课程2023-2024-1《计算机基础与程序设计》这个作业要求在哪里2023-2024-1《计算机基础与程序设计》这个作业的目标学习计算机科学概论第4章第5章和《C语言程序设计......
  • 【2023最新教程】有道翻译js 超详细!!!
    目录前言确定加密字段破解加密字段加密字段实现破解返回值跟踪堆栈找到解密后的返回值函数实现解密返回值语言转换完整代码效果展示前言有道翻译两个加密第一个是表单的sign用MD5加密可以点击目录的加密字段实现直接跳转第二个是返回值AES加密,而且AES的密匙他还用md5加......
  • 2023 CCPC 秦皇岛站赛后总结 -lhy
    这次ccpc开始前就隐隐有些不安,果然最后不出所料打铁了。开始前一晚vp了一场20年的区域赛,按照当年的排名也就是铜牌中等的水平。也算是打了打气。比赛开始时我们先一块看了G题,确实是签到,zzh手快就直接a了。然后我看J题交的和过的人比较多,就去看J。zzh一眼看出来是小猫爬山的原题,但......
  • 2023-2024-1 20231305 《计算机基础与程序设计》第4周学习总结
    2023-2024-120231305《计算机基础与程序设计》第4周学习总结作业信息这个作业属于哪个课程<班级的链接>(如2022-2023-1-计算机基础与程序设计)这个作业要求在哪里<作业要求的链接>(如2022-2023-1计算机基础与程序设计第一周作业)这个作业的目标<写上具体方面>......
  • [题解]P9752 [CSP-S 2023] 密码锁
    这次CCF的行为过于迷惑了。思路首先发现只会有\(10^5\)种密码,考虑枚举它们,然后去check。假设当前密码是:\(p_1,p_2,p_3,p_4,p_5\)。如果它能从对于所有\(1\simn\)种错误的密码按照题目所述的操作得到,那么此密码就是合法的。假设我们现在判断当前密码能否由第\(i\)种......
  • 2023.9.20
    在Java项目开发中,异常处理是至关重要的一部分。合理的异常处理可以提高代码的可靠性和可维护性,确保程序在发生异常时能够正确地进行处理。下面总结了一些常见的异常处理情况,以便参考:1.NullPointerException(空指针异常):常见原因是对一个为null的对象进行操作。为了避免这种异常,......
  • 2023-2024-1 20231309 《计算机基础与程序设计》第四周学习总结
    作业信息这个作业属于哪个课程2023-2024-1-计算机基础与程序设计这个作业要求在哪里2023-2024-1计算机基础与程序设计第四周作业这个作业的目标作业正文2023-2024-120231309《计算机基础与程序设计》第四周学习总结教材学习内容总结本周主要学习了C语言......
  • 2023-2024-1 20231319《计算机基础与程序设计》第四周学习总结
    2023-2024-120231319邓传山《计算机基础与程序设计》第3周学习总结作业信息这个作业属于哪个课程2023-2024-1-计算机基础与程序设计这个作业要求在哪里https://www.cnblogs.com/rocedu/p/9577842.html#WEEK01这个作业的目标学习《计算机科学概论(第7版)》第4、5章......