switch case 注意事项

switch case常见的注意事项:
          1.case后面常量值的顺序可以任意,一般按顺序编写
          2.default顺序也可以编写在switch中的任意位置
           当所有case都不满足时则执行default
           建议default编写在所有case的后面
          3.break是可有可无的
           当没有编写break,则从当前第一个匹配的case一直向下执行(也就是穿透)

                  例子↓

        
           因此,根据题意适当编写break,否则会根据所输入的情况执行下去

 

 1 switch (info){
 2     case "第二名":
 3         System.out.println("奖励10w");
 4         
 5     case "第一名":
 6         System.out.println("奖励5w");
 7     default:
 8         System.out.println("对不起,无奖励");
 9         
10     case "第三名":
11         System.out.println("奖励2w");
switch case例子

 

posted @ 2019-03-19 16:11  Penphy  阅读(525)  评论(0编辑  收藏  举报