switch case 穿透 示例

2023-06-26,,

public class SwitchCase {
//判断输入的月份属于第几季度
public static void main(String[] args) {
//随机获得 1-12个月份中的一个值
int num =(int)(Math.random()*12+1);
//利用switch case 穿透
switch (num){
case 1:
case 2:
case 3:
System.out.println("第一季度");
break;
case 4:
case 5:
case 6:
System.out.println("第二季度");
break;
case 7:
case 8:
case 9:
System.out.println("第三季度");
break;
case 10:
case 11:
case 12:
System.out.println("第四季度");
break;
} }
}

查看代码1

package test;

public class Switch {
//判断给定日期属于一年中的第几天
public static void main(String[] args) {
String date = "2024-12-31";
String year = date.substring(0, 4);
int yy = Integer.parseInt(year);
String month = date.substring(5, 7);
int mm = Integer.parseInt(month);
String day = date.substring(8, 10);
int dd = Integer.parseInt(day);
int sum = dd;
switch (mm) {
case 12:
sum += 30;
case 11:
sum += 31;
case 10:
sum += 30;
case 9:
sum += 31;
case 8:
sum += 31;
case 7:
sum += 30;
case 6:
sum += 31;
case 5:
sum += 30;
case 4:
sum += 31;
case 3:
if (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) {
sum += 29;
} else {
sum += 28;
}
case 2:
sum += 31;
case 1:
sum += 0;
break; }
System.out.println(date+"是"+yy+"年第"+sum+"天");
}
}

查看代码2

switch case 穿透 示例的相关教程结束。

《switch case 穿透 示例.doc》

下载本文的Word格式文档,以方便收藏与打印。