一、while & do...while & switch模板代码
1.1、while
/**
* 需求:使用while循环打印5遍Hello World!
*/
@Test
public void print5() {
int i = 1;
while (i <= 5) {
System.out.println("Hello World! " + LocalDateTime.now());
// 线程休眠(单位:秒)
try { TimeUnit.SECONDS.sleep(1); } catch (Exception e) {e.printStackTrace();}
i++;
}
}
1.2、do...while
/**
* 需求:使用do...while循环打印10遍你好世界!
*/
@Test
public void print10() {
int i = 1;
do {
System.out.println("你好世界! " + LocalDateTime.now());
// 线程休眠(单位:秒)
try { TimeUnit.SECONDS.sleep(1); } catch (Exception e) {e.printStackTrace();}
i++;
} while (i <= 10);
}
1.3、switch
/**
* 根据输入的weekDay,输出具体的日期信息
*/
@Test
public void switchDateTest() {
// Monday、Tuesday、Wednesday、Thursday、Friday、Saturday、Sunday
String weekDay = "Wednesday2";
switch (weekDay) {
case "Monday":
System.out.println("今天星期一");
break;
case "Tuesday":
System.out.println("今天星期二");
break;
case "Wednesday":
System.out.println("今天星期三");
break;
case "Thursday":
System.out.println("今天星期四");
break;
case "Friday":
System.out.println("今天星期五");
break;
case "Saturday" :
System.out.println("今天星期六");
break;
case "Sunday":
System.out.println("今天星期日");
break;
default:
System.out.println("日期不对");
}
}