输入某年某月某日,判断这一天是这一年的第几天?(考虑闰年的情况)public class Day1 {
public static void main(String[]args){
System.out.println("请输入某年:");
Scanner in = new Scanner(System.in);
int year = in.nextInt();
System.out.println("请输入某月:");
int month = in.nextInt();
System.out.println("请输入某日:");
int day = in.nextInt();
int arr1[] = {31,28,31,30,31,30,31,31,30,31,30,31};//非闰年
int arr2[] = {31,29,31,30,31,30,31,31,30,31,30,31};//闰年
int tian = 0;
int zhengyue = 0;
if(year%100!=0&&year%4 == 0){
if(month<2){
System.out.println(year+"年"+month+"月"+day+"日是今年的第"+day+"天"); }
else
{
for(int i = 2;i <= month;i++)
{ zhengyue += arr2[i-2]; }
tian = zhengyue + day;
System.out.println(year+"年"+month+"月"+day+"日是今年的第"+tian+"天");}
}
else{
if(month<2)
{ System.out.println(year+"年"+month+"月"+day+"日是今年的第"+day+"天"); }
else{ for(int i = 2;i <= month;i++)
{
zhengyue += arr1[i-2];
}
tian = zhengyue + day;
System.out.println(year+"年"+month+"月"+day+"日是今年的第"+tian+"天");
}
}
}
}
|
|