这里写自定义目录标题
计算天数:
输入年月日,计算该填是本年的第几天。例如1990 年9 月20 日是1990 年的第263 天,2000 年5 月1 日是2000 年第122 天。
import java.util.Scanner;
public static void main(String[] args) {
int num[] = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//定义数组,数组包含每个月的日期
System.out.println("请输入年份:");
Scanner a = new Scanner(System.in);
int year = a.nextInt(); //输入年份
System.out.println("请输入月份:");
Scanner b = new Scanner(System.in);
int month = b.nextInt(); //输入月份
System.out.println("请输入日期:");
Scanner c = new Scanner(System.in);
int day = c.nextInt(); //输入日期
if (year % 4 == 0 && year % 100 != 0) { //算出闰年 闰年只可以被4整除,但是不能被100整除的数为闰年
num[1] = 29; //如果为true则给num[1]赋值为29,也就是2月闰年为29天
} else if (year % 400 == 0) { //算出世纪闰年,只能被400整除,如果为true则为闰年,2月有29天
num[1] = 29; //如果为true则为闰年,2月有29天
} else {
num[1] = 28; 如果以上if都没为true则2月为28天
}
month--; //数组开始索引是0,而定义的数组天数是从一月开始算起 所以这里month(月份)要-1
int end = 0;
if (month <= 12) { //判断输入的月份是否输入规范
for (int i = 0; i < month; i++) { //循环索引,i直到输入的月份为止
end = num[i] + end; //循环加,输入的月份之前的数值
}
if (day <= num[month]) { //判断输入的日期是否合理
end += day; //如果为true则end加上输入的日期
System.out.println(year + "年第" + end + "天"); //打印最终结果
} else {
System.out.println("输入错误的日期"); //如果日期为false则打印输出
}
} else {
System.out.println("输入错误的月份");//如果月份为false则打印输出
}
}
}