• 分支结构-Switch


    /*
        switch(表达式或变量){
            case value1:{
                语句体1;
                break;
            }
            case value2:{
                语句体2;
                break;
            }
            ...
            default:{
                语句体n+1;
                break;
            }
        }
    */
    public class SwitchDemo{
        public static void main(String[] args){
            int i = 1;
            long lon = 10L;
            byte b = 10;
            short s = 10;
            String str = "abc";
            switch(str){
                case "abc":{
                    System.out.println("abc");
                    break;
                }
                
                case "bcd":{
                    System.out.println("bcd");
                    break;
                }
                
                /*
                case 1:{
                    System.out.println("i == 1");
                    // break;//贯穿,跳过下面的case语句匹配
                }
                case 5:{
                    System.out.println("i == 5");
                    break;
                }
                case 10:{
                    System.out.println("i == 10");
                    break;
                }
                default:{
                    System.out.println("default");
                    break;
                }
                */
                
            }
            
            System.out.println("其它语句");
        }
    }
    /*
    从键盘输入月份数字,显示是第几个季度
    */
    import java.util.Scanner;
    
    public class SwitchDemo2{
        public static void main(String[] args){
            Scanner s = new Scanner(System.in);
            System.out.print("请输入月份:");
            int month = s.nextInt();
            
            switch(month){
                case 1:{
                    // System.out.println(month + "属于第一季度");
                    // break;
                }
                case 2:{
                    // System.out.println(month + "属于第一季度");
                    // break;
                }
                case 3:{
                    System.out.println(month + "属于第一季度");
                    break;
                }
                case 4:{
                    // System.out.println(month + "属于第二季度");
                    // break;
                }
                case 5:{
                    // System.out.println(month + "属于第二季度");
                    // break;
                }
                case 6:{
                    System.out.println(month + "属于第二季度");
                    break;
                }
                case 7:{
                    // System.out.println(month + "属于第三季度");
                    // break;
                }
                case 8:{
                    // System.out.println(month + "属于第三季度");
                    // break;
                }
                case 9:{
                    System.out.println(month + "属于第三季度");
                    break;
                }
                case 10:{
                    // System.out.println(month + "属于第四季度");
                    // break;
                }
                
                case 11:{
                    // System.out.println(month + "属于第四季度");
                    // break;
                }
                case 12:{
                    System.out.println(month + "属于第四季度");
                    break;
                }
                default :{
                    System.out.println("输入的月份有误");
                    break;
                }
            }
    
        }
    }
    /*从键盘输入消费金额,显示原金额和折扣价
    200以下没有折扣;? 0
    200-399九折;? 1
    400-599八折;? 2
    600往上七折;? 3 ...
    */
    import java.util.Scanner;
    
    public class SwitchDemo3{
        public static void main(String[] args){
            Scanner s = new Scanner(System.in);
            System.out.print("请输入消费金额: ");
            int money = s.nextInt();
            //定义折扣率
            double discount = 1.0;
            
            if(money < 0){
                System.out.println("消费金额不能为负");
            }else{
                switch(money / 200){
                    case 0:{
                        break;
                    }
                    case 1:{
                        discount = 0.9;
                        break;
                    }
                    case 2:{
                        discount = 0.8;
                        break;
                    }
                    default :{
                        discount = 0.7;
                        break;
                    }
                }
                System.out.println("折扣前: " + money + ",折扣后金额:" + (money * discount));
            }
            
        }
    }
  • 相关阅读:
    如何配置寄存器
    逻辑分析仪的使用
    CAN中如何计算波特率并配置波特率
    led不同颜色的驱动电压和驱动电流
    ULINK2配置
    电机加减速转动
    2N7002
    未添加时钟文件产生报错
    烟雾传感器
    Strategy 设计模式 策略模式 超靠谱原代码讲解
  • 原文地址:https://www.cnblogs.com/leo9257/p/8733623.html
Copyright © 2020-2023  润新知