用java 判断这一天是这一年的第几天?输入某年某月某日判断这一天是这一年的第几天?请输入年:2019请输入月:5请输入日:29第149天
以下是Java代码实现:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入年:");
int year = input.nextInt();
System.out.print("请输入月:");
int month = input.nextInt();
System.out.print("请输入日:");
int day = input.nextInt();
int[] daysOfMonth = {31,28,31,30,31,30,31,31,30,31,30,31}; // 每个月的天数
int days = day; // 初始化为当月的天数
for (int i = 0; i < month - 1; i++) { // 累加前面月份的天数
days += daysOfMonth[i];
}
if (month > 2 && isLeapYear(year)) { // 如果是闰年且当前月份在3月及以后,要加上闰年的一天
days++;
}
System.out.println("第" + days + "天");
}
// 判断是否为闰年
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
}
``
原文地址: https://www.cveoy.top/t/topic/cHUa 著作权归作者所有。请勿转载和采集!