java 的一道题
- 提问者网友:斑駁影
- 2021-05-17 02:57
1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
- 五星知识达人网友:逐風
- 2021-05-17 03:14
import java.util.Scanner;
//题目:输入某年某月某日,判断这一天是这一年的第几天
public class ThisDayIs {
public static void main(String[] args) {
System.out.println("请输入年份,如:2008");
Scanner YMD = new Scanner(System.in);
int year = YMD.nextInt();
System.out.println("请输入月份1~12,如8");
int month = YMD.nextInt();
System.out.println("请输入天数1~31,如8");
int day = YMD.nextInt();
if (LeapYear(year)) {
switch (month) {
case 1: {
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 2: {
day = day + 31;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 3: {
day = day + 29 + 31;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 4: {
day = day + 29 + 31 * 2;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 5: {
day = day + 29 + 31 * 2 + 30;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 6: {
day = day + 29 + 31 * 3 + 30;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 7: {
day = day + 29 + 31 * 3 + 30 * 2;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 8: {
day = day + 29 + 31 * 4 + 30 * 2;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 9: {
day = day + 29 + 31 * 5 + 30 * 2;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 10: {
day = day + 29 + 31 * 5 + 30 * 3;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 11: {
day = day + 29 + 31 * 6 + 30 * 3;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 12: {
day = day + 29 + 31 * 6 + 30 * 4;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
}
} else
switch (month) {
case 1: {
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 2: {
day = day + 31;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 3: {
day = day + 28 + 31;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 4: {
day = day + 28 + 31 * 2;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 5: {
day = day + 28 + 31 * 2 + 30;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 6: {
day = day + 28 + 31 * 3 + 30;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 7: {
day = day + 28 + 31 * 3 + 30 * 2;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 8: {
day = day + 28 + 31 * 4 + 30 * 2;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 9: {
day = day + 28 + 31 * 5 + 30 * 2;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 10: {
day = day + 28 + 31 * 5 + 30 * 3;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 11: {
day = day + 28 + 31 * 6 + 30 * 3;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
case 12: {
day = day + 28 + 31 * 6 + 30 * 4;
System.out.println("这是" + year + "的第" + day + "天");
break;
}
}
}
private static boolean LeapYear(int year) {
if (year % 4 == 0 || year % 100 == 0){
System.out.print(year+"是闰年");
return true;
}
else
return false;
}
}