Java判断闰年平年

 

 

 

 

 

 

 

 

package leap;
import java.util.Scanner;
public class Leap {
public static void main(String args[])
{
int year; //年
int month; //月
int day = 0; //日
boolean flag; //判断标志
System.out.print("请依次输入年和月:"+'\n');
Scanner sc=new Scanner(System.in);
year=sc.nextInt();
month=sc.nextInt();
if(year%400==0||(year%4==0&&year%100!=0))
{
flag=true;
System.out.println(year+"年是闰年!");
}

else {
flag=false;
System.out.println(year+"年是平年!");
}
if(flag==true)
{
switch(month)
{
case 1:
day=31;
break;
case 2:
day=29;
break;
case 3:
day=31;
break;
case 4:
day=30;
break;
case 5:
day=31;
break;
case 6:
day=30;
break;
case 7:
case 8:
day=31;
break;
case 9:
day=30;
break;
case 10 :
day=31;
break;
case 11:
day=30;
break;
case 12:
day=31;
break;
}
System.out.println("且"+year+"年的"+month+"月有"+day+"天");
}
else
{
switch(month)
{
case 1:
day=31;
break;
case 2:
day=28;
break;
case 3:
day=31;
break;
case 4:
day=30;
break;
case 5:
day=31;
break;
case 6:
day=30;
break;
case 7:
case 8:
day=31;
break;
case 9:
day=30;
break;
case 10 :
day=31;
break;
case 11:
day=30;
break;
case 12:
day=31;
break;
}
System.out.println("且"+year+"年的"+month+"月有"+day+"天");
}
}
}

posted @ 2020-07-21 22:44  靠谱杨  阅读(253)  评论(0编辑  收藏  举报