程序流程控制

2019-06-26  本文已影响0人  南国铃子

1. if或多重if判断 更适合于区间判断;

2. switch 分支判断 更适合于等值判断;

//byte,int,short,long,string

----------------------------------

class Demo2{

public static void main(String[] args){

int grade = 3;

switch(grade){

case 1:

  System.out.println("*");

  break;

case 2:

  System.out.println("**");

  break;

case 3:

  System.out.println("***");

  break;

default:

  System.out.println("no grade");

  break;

}

}

}

------------------------------

---------------switch 穿透作用

class Demo2{

public static void main(String[] args){

int year = 2019;

int month = 1;

int day = -1;

switch(month){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

  day = 31;

  break;

case 4:

case 6:

case 9:

case 11:

  day = 30;

  break;

case 2:

  if(year % 4 == 0 || (year % 4 == 0 && year % 100 != 0)){

  day = 29;

  }else{

  day =28;

  }

default:

  System.out.println("faults");

  break;

}

if(day != -1){

System.out.println(year+""+month+""+day);

}

}

}

上一篇下一篇

猜你喜欢

热点阅读