代码练习--switch


public class switcha {
    public static void main(String[] args ){
    /*例子    
    int i = 123;
    switch (i) {
    case 0:
            System.out.println("zero");
            break;
    case 1:
            System.out.println("one");
            break;
    case 2:
            System.err.println("two");
            break;
    default:
        System.out.println("default");
        break;
    }
    */
        
    /*输入季节,输出季节特色    
    String season = "abc";
    
    switch(season){
        case "spring":
                System.out.println("春暖花开");
                break;
        case "summer":
                System.out.println("夏日炎炎");
                break;
        case "autumn":    
                System.out.println("秋高气爽");
                break;
        case "winter":
                System.out.println("冬雪皑皑");
                break;
        
        default:
                System.out.println("季节输入有误!,请重新输入:");
                break;
    }*/
        
    /*例题1、把小写类型的char型转为大写,只转换a,b,c,d,其他的输出“other”     
        char i = 'f';
        switch(i){
            case 'a':
                System.out.println("A");
                break;
            case 'b':
                System.out.println("B");
                break;
            case 'c':
                System.out.println("C");
                break;
            case 'd':
                System.out.println("D");
                break;
            default:
                System.out.println("Other");
                break;
        }
        */
        
        /*例题2、对学生成绩大于60分的,输出“合格”;低于60分的,输出“不合格”
        
        int score = 70;
        int a = (score >= 60) ? 1:0;
        switch(a){
            case 1 :
                System.out.println("合格");
                break;
            case 0:
                System.out.println("不合格");
                break;
        }
        */
    
    /*例题3、根据指定的月份,打印该月份所属的季节:3,4,5-春天;6,7,8-夏天;
     * 9,10,11-秋季;12,1,2-冬季
        int month = 5;
        switch(month){
        case 3:
        case 4:
        case 5:
            System.out.println("春天");
            break;
            
        case 6:
        case 7:
        case 8:
            System.err.println("夏天");
            break;
        case 9:
        case 10:
        case 11:
            System.out.println("秋天");
            break;
        case 12:
        case 1:
        case 2:
            System.out.println("冬天");
            break;
        default:
            System.out.println("请输入正确的月份!");
            break;
        }
*/
    /**if 语句 化为 switch语句*/
    //if语句    
     int a = 3;//初始化a的值
      int x = 100;
      if(a==1){
        x+=5;
          System.out.println(x);
          }
      else if(a==2){
        x+=10;
        System.out.println(x);
     }
      else if(a==3){
        x+=16;
        System.out.println(x);
     }
      else    {    
        x+=34;
        System.out.println(x);
      }
      
      //switch语句
        int b =3;//初始化b的值
        int y = 100;
        switch(b){
            case 1:
                x += 5;
                System.out.println(y);
                break;
            case 2:
                x += 10;
                System.out.println(y);
                break;
            case 3:
                x += 16;
                System.out.println(y);
                break;
            default:    
                x += 34;
                System.out.println(y);  
                break;
        }
        
        
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值