软件园学生在线

  • {{ item.name }}
  • 2023试用期

登录与注册

后端1 叶亦雯

  • [email protected]
  • 2023-10-29
  • 0

# 学习笔记
## Spring boot的代码

1.新建一个项目,选择Spring Initializr,type选moven,web选Spring Web
2.新建一个controller的package(.controller)
3.开始在这个包下搓代码
@RestController//一个接口,让浏览器能直接发送请求到你写的后端
@RequestMapping("/")//指定路由地址
public class hhhController {
    @GetMapping("/hello")
    public String hello(){
        return"Hello World";
    }
4.搓个能交互且美观的???
//位于Service类中
@Service//一种用于标记服务层Bean的注解,并将其对象存入Spring容器中
public class hhhService {
    public String haha (String game)
    {
        if(game.equals("bengtie")){
            return"启动";
        }else{
            return"滚粗";
        }
    }
    ---------------------------
       public Double add(Double a,Double b){
        return a+b;//大写是对象,小写是数据类型
    }
//位于Controller类中
@RestController
@RequestMapping("/")
public class hhhController {
  @Autowired//可以对类成员变量,方法及构造函数进行标注,完成自动装配的工作
  hhhService hhhService;

  @PostMapping("/game")//Post是方法
  public String hhh(@RequestParam String game){
       return hhhService.haha(game);
  }
  ------------------------
  @RequestMapping(value="/add",method=RequestMethod.GET)
  public Double add(@RequestParam Double a,@RequestParam Double b){
        return hhhService.add(a,b);
  }
  ```
  ```mark
  5.网页跑一下,地址:localhost:8080/game
                     localhost:8080/add?a=1&b=2//问号指示参数开始        

# 狗屁不通的四则运算

下面有进修后版本,虽然不知道尊贵的学长学姐会不会细看,但还是想说这里推荐闭着眼看 :(
 public static double main  (String[]args){
        Scanner sc=new Scanner(System.in);//从键盘读取用户输入
        System.out.println("欢迎使用拉跨的四则运算器");
        System.out.println("请输入一行式子,包含第一个整数,运算符,第二个整数,例如1+2");
        double x=0,y=1,result=0;//以防万一,赋个初值
        char op;
        String str;
        x =sc.nextDouble();
        str=sc.next();
        op=str.charAt(0);
        y=sc.nextDouble();
        switch(op){
            case'+':
                result=x+y;
                break;
            case'-':
                result=x-y;
                break;
            case'*':
                result=x*y;
                break;
            case'/':
                result=x/y;
                break;
            default:

                System.out.print("fault");
        }
        return result;
        -----------------------
        @Autowired
    Operation Operation;
    @RequestMapping(value="/Operation",method=RequestMethod.GET)
    public static void main(@RequestParam String[]args)
    {return Operation.main(result);}
    //怎么调也调不对放弃了,尽力了
```java
------------long time later-----------------------------------

//有时间进修了,赶脚又觉滴自己可以了(主打个盲目自信
@Service

public class Operation {
public double hh (Double x,char op,Double y){
Scanner sc=new Scanner(System.in);//从键盘读取用户输入
System.out.println("欢迎使用拉跨的四则运算器");
System.out.println("请输入一行式子,包含第一个整数,运算符,第二个整数,例如1+2");
Double result=1.0;//以防万一,赋个初值

    String str;
    x =sc.nextDouble();//等于new Sanner(System.in)
    str=sc.next();//输入
    op=str.charAt(0);//取第1个位置的字符
    y=sc.nextDouble();
    switch(op){
        case'+':
            result=x+y;
            break;
        case'-':
            result=x-y;
            break;
        case'*':
            result=x*y;
            break;
        case'/':
            result=x/y;
            break;
        default:

            System.out.print("fault");
    }
    return result;

@RequestMapping(value="/Operation",method=RequestMethod.GET)
public Double hh(@RequestParam Double x,@RequestParam char op,@RequestParam Double y)
{return Operation.hh(x,op,y);}


    收获:之前的一个报错Expected 0 bur found,浅浅了解了有参构造和无参构造
    产品说明:这是个狗屁不通的四则运算,由于作者社团忙成狗了,也没有任何高大威猛的扩展,使用方法见上,就酱
202300300347@mail.sdu.edu.cn
[email protected]
© 2025 软件园学生在线
Theme by Wing