zoukankan      html  css  js  c++  java
  • 分支结构-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));
            }
            
        }
    }
  • 相关阅读:
    网站常见的反爬虫和应对方法
    网站反爬虫
    webmagic的设计机制及原理-如何开发一个Java爬虫
    腾讯社区搜索架构演进
    搜索引擎评估与互联网用户行为建设
    深层网络搜索核心技术研讨
    Solr与Cassandra二级缓存实践
    .NET的前世今生与将来
    技术人生的职场众生相
    爆栈之旅
  • 原文地址:https://www.cnblogs.com/leo9257/p/8733623.html
Copyright © 2011-2022 走看看