帮我用spring boot 写一个计算器程序
时间: 2023-11-25 15:05:12 浏览: 34
好的,以下是一个简单的计算器程序的示例代码,使用Spring Boot框架实现:
1. 创建一个新的Spring Boot项目,添加以下依赖项:
```
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
```
2. 创建一个Controller类,用于处理HTTP请求:
```
@RestController
public class CalculatorController {
@GetMapping("/add")
public int add(@RequestParam int a, @RequestParam int b) {
return a + b;
}
@GetMapping("/subtract")
public int subtract(@RequestParam int a, @RequestParam int b) {
return a - b;
}
@GetMapping("/multiply")
public int multiply(@RequestParam int a, @RequestParam int b) {
return a * b;
}
@GetMapping("/divide")
public double divide(@RequestParam double a, @RequestParam double b) {
return a / b;
}
}
```
3. 启动应用程序,访问以下URL测试:
```
http://localhost:8080/add?a=2&b=3
http://localhost:8080/subtract?a=5&b=2
http://localhost:8080/multiply?a=3&b=4
http://localhost:8080/divide?a=10&b=2
```
这个程序提供了四个基本的数学运算:加法、减法、乘法和除法。你可以根据需要添加更多的运算。