java编写万年历(要求输入年份和月份打印这个月的日历)
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个年份:");
//接收年份
int year=sc.nextInt();
System.out.println("请输入一个月份:");
//接收月份
int month=sc.nextInt();
//累计跨年的天数
int yearTotal=0;
//计算跨年的天数(当年的不能算,不然整年的天数都加上了)
for(int i=1900;i<year;i++){
//闰年加366
if(i%4==0&&i%100!=0||i%400==0){
yearTotal+=366;
}
//平年加365
else{
yearTotal+=365;
}
}
//累计跨月总天数
int monthTotal=0;
//记录每月的天数
int day=0;
//i<=month方便后面计算的当月有多少天,但是计算这个月之前总天数时,不能加上本月,所以后面加总的时候要判断
for(int i=1;i<=month;i++){
switch(i){
case 2:day=year%4==0&&i%100!=0||i%400==0?29:28;
break;
case 4:
case 6: