import java.util.Scanner;
/*描述
描述
如题,输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第几天。
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每行的输入数据都是一个按题目要求格式输入的日期。
输出
每组输入数据的输出占一行,输出判断出的天数n
样例输入
3
2000 4 5
2001 5 4
2010 10 24
样例输出
96
124
297
*/
class 判断日期是该年的第几天 {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
System.out.println(getDays(year, month, day));
}
private static int getDays(int year, int month, int day) {
//如果日期不规范
if (year < 0 || month <= 0 || month > 12 || day <= 0 || day > 31) {
return -1;
}
int days = 0;//天数
switch (month - 1) {//累加本月之前的天数
case 11:
days += 30;
case 10:
days += 31;
case 9:
days += 30;
case 8:
days += 31;
case 7:
days += 31;
case 6:
days += 30;
case 5:
days += 31;
case 4:
days += 30;
case 3:
days += 31;
case 2:
days += isLeapYear(year) == true ? 29 : 28;//若是闰年则+ 29,否则+ 28
case 1:
days += 31;
}
days += day;//加上本月天数
return days;
}
//判断是否为闰年
private static boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
}
java判断日期是该年的第几天switchcase实现
最新推荐文章于 2023-02-27 15:06:30 发布