用switch语句输出一个月有多少天
import java.time.Month;
import java.time.Year;
public class Switch2 {
public static void main(String[] args) {
Year y = Year.of(2020);
Month m = Month.of(2);
int m1 = m.getValue();
int days = 31;
switch (m1) {
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = y.isLeap() ? 29 : 28;
break;
default:
days = 31;
break;
}
System.out.printf("%d年%d月有%d天%n", y.getValue(), m1, days);
}
}
用switch语句输出今天是星期几
import java.util.Date;
public class Switch1 {
public static void main(String[] args) {
var d = new Date();
var k = d.getDay();
switch (k) {
case 0:
System.out.println("周日");
break;
case 1:
System.out.println("周一");
break;
case 2:
System.out.println("周二 ");
break;
case 3:
System.out.println("周三");
break;
case 4:
System.out.println("周四");
break;
case 5:
System.out.println("周五");
break;
default:
System.out.println("周六");
break;
}
}
}