最近要考试,还有蓝桥作业,比较忙

1.先判断输入的日期是否为闰年
2.在判断输入的月份是否为2月
3.在获取输入的年份和月份的1月1日 的列数
4.在输出

import java.util.*;
public class demo{
    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();
        
        //获取输入的某年某月1号对应的列数
        Calendar c=Calendar.getInstance();
        c.set(year,month-1,1);
        int week=c.get((Calendar.DAY_OF_WEEK));
        System.out.println(week);
        
        
        //判断年份和月份
        int day;
        if(month==1||month==3||month==5||month==7 ||month==8||month==12){
            day=31;
        }else if(month==4||month==6||month==9||month==11){
            day=30;
        }else if(month==2&&year%400==0||year%4==0&&year%100!=0){
                day=29;
        }else{
            day=28;
        } 
        
        
        int count=0;//定义一个列数
        System.out.println("日 一 二 三 四 五 六");
        for(int j=1;j<week;j++){
            System.out.print("  "+" ");
            count++;
        }
        for(int i=1;i<=day;i++){ //day循环
            if(i<10){
                System.out.print(" "+i+" ");
                count++;
            }else {
                System.out.print(i+" ");
                count++;
            }
            
            if(count%7==0){   //一行七列
                System.out.println();
                continue;
            }
        }   
    }
}
posted @ 2017-12-03 23:14  宋建楠  阅读(215)  评论(0)    收藏  举报