枚举enum简单响应码案例

状态码枚举类代码:

package demo;

public enum ERROR {
    

    SUCCESS(200,"响应成功"),
    ERR(500,"响应失败");
    
    private int code;
    private String message;
    
    ERROR(int code,String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

测试案例:

package demo;

import java.util.Scanner;

public class R{
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        if (str.equals("yes")) {
            System.out.println(ERROR.SUCCESS.getMessage());
        }else if(str.equals("no")){
            System.out.println(ERROR.ERR.getMessage());
        }
    }
}

运行结果:(输入yes和no)

 

 

 

posted @ 2020-10-20 14:41  LW_20171224  阅读(313)  评论(0编辑  收藏  举报