Java Switch和有趣代码

Java Switch和有趣代码

switch详解

switch…case结构:多条路

  • 说明:

  • 优点:效率高、结构清晰

  • 缺点:只能对整数判断相等

  • break:跳出switch

  • 没有break就会向下一直运行,直到结束

  • 如果都不符合用default

  • switch可以作用于哪些数据类型上

—byte、short、int、char、String、枚举类型(enum)-------------重要

有趣代码

Haha我自己写的觉得有趣的代码

//银行流程
public static void main(String[] args) {
    
    
    Scanner scan = new Scanner(System.in);
    int number = 1;
    System.out.println("请选择你的初始资金:");
    int cash_Saved = scan.nextInt();
    while(number==1) {
    
    
        System.out.println("请选择功能: 1.取款, 2存款, 3.查询余额 0.退卡");
        int command = scan.nextInt();
        switch (command) {
    
    
            case 0: //0.退卡
                System.out.println("好的您的余额是:" + cash_Saved);
                System.out.println("现在退卡");
                number=0;
                break;
            case 1: //1.取款
                System.out.println("你要取多少钱:");
                int cash_Out = scan.nextInt();
                System.out.println("好的现在为您取!!");
                cash_Saved = cash_Saved-cash_Out;
                break;
            case 2: //2存款
                System.out.println("你要存多少钱:");
                int cash_In = scan.nextInt();
                System.out.println("好的现在为您存!!");
                cash_Saved = cash_Saved + cash_In;
                System.out.println("好的您的余额是:" + cash_Saved);
                break;
            case 3: //3.查询余额
                System.out.println("好的您的余额是:" + cash_Saved);
                break;
            default:
                System.out.println("输入错误请重新再输一遍");
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/gydennis/article/details/122320403