import java.util.Scanner;
/*输入某年某月某日,判断这一天是这一年的第几天?*/
public class GetDate {
public static void main(String[] args) {
// TODO Auto-generated method stub
int year, month, day;
int sum = 0;//求第几天
int ordinary_year[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//平年各月天数
int leap_year[] = { 31,29,31,30,31,30,31,31,30,31,30,31 };//闰年各月天数
System.out.print("输入哪一年:");
Scanner sc1=new Scanner(System.in);
year=sc1.nextInt();
System.out.print("输入哪一月:");
Scanner sc2=new Scanner(System.in);
month=sc2.nextInt();
System.out.print("输入哪一日:");
Scanner sc3=new Scanner(System.in);
day=sc3.nextInt();
if (year % 4 == 0)//闰年
{
for (int i = 0;i <month - 1;i++)
{
sum = sum + leap_year[i];
}
sum = sum + day;
}
else if (year % 4 != 0)//平年
{
for (int j = 0;j < month - 1;j++)
{
sum = sum + ordinary_year[j];
}
sum = sum + day;
}
System.out.print("这是:"+year+"年的第"+sum+"天\n");
sc1.close();
sc2.close();
sc3.close();
}
}
JAVA经典50题(14)
Java日期计算:找出一年中的第几天
最新推荐文章于 2023-07-23 15:30:34 发布
本文介绍了一个Java程序,该程序能够接收用户输入的年、月、日,然后计算并显示这一天是一年中的第几天。程序考虑了平年和闰年的不同情况,通过数组分别存储了平年和闰年每个月的天数。
10万+

被折叠的 条评论
为什么被折叠?



