分支语句的注意事项

switch建议判断固定值的时候用

if建议判断区间或范围的时候用


 

public static void main(String[] args) {
      boolean b = true;
      if (b == false)
         System.out.println("a");
      else if (b)
         System.out.println("b");

      else if (!b)
         System.out.println("c");
      else
         System.out.println("d");
   }

 运行结果:b


public static void main(String[] args) {

      int x = 2;

      int y = 3;

      switch (x) {

      default:

         y++;

      case 3:

         y++;

      case 4:

         y++;

      }

      System.out.println("y=" + y);

   }

 输出结果为:y=6

原文地址:https://www.cnblogs.com/loaderman/p/6403454.html