SCAU 输出指定年份的日历

练习3:输出指定年份的日历

内容要求:

编写程序,实现如下功能:

提示用户输入一个年份和代表该年第一天是星期几的数字。说明星期日到星期六用0-6表示。

程序依次输出该年的每个月的日历。

运行实例:
输入年份: 2017
该年第1天是星期几: 0

package calendar;

//import java.sql.SQLOutput;
import java.util.Scanner;

public class practice {
     
    public static void main(String[] args) {
     
        Scanner cin = new Scanner(System.in);
        System.out.print("输入年份:");
        int year = cin.nextInt();
        System.out.print("该年第1天是星期几:");
        int n = cin.nextInt();
        for(int i=1;i<=12;i++)
        {
     
            System.out.printf("%d年  %d月\n",year,i);
            System.out.println("------------------------------");
            System.out.println("Sun Mon Tue Wed Thu Fri Sat");
            for(int k=0;k<n;k++){
     
                System.out.print("    ");
            }
            int date = 1;
            boolean sit = false;
            int[] month = new int[13];
            month[1]=month[3]=month[5]=month[7]=month[8]=month[10]=month[12]=31;
            month[9]=month[4]=month[6]=month[11]=30;
            if(year%4==0)month[2]=29;
            else month[2]=28;
            while(date<=month[i])
            {
     
                for(int j=n;j<7;j++){
     
                    System.out.printf("%4d",date);
                    ++date;
                    if(date==month[i]+1){
     
                        if(j==6){
     
                            n=0;
                        }
                        else n=j+1;
                        sit=true;
                        break;
                    }
                }
                System.out.println();
                if(!sit)n=0;
            }
            //System.out.println();
            //System.out.printf("%d",n);
            System.out.println("------------------------------");
        }

    }
}

你可能感兴趣的:(Java基础)