输入年月,输出该月份的日历页

import java.util.Calendar;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Calendar c=Calendar.getInstance();
        Scanner read=new Scanner(System.in);
        int year=read.nextInt();
        int month=read.nextInt();
        c.set(year,month-1,1);
        int xinqi=c.get(Calendar.DAY_OF_WEEK)-1;
        String a []=new String [xinqi+31];
        int length=0;
             switch(month)
             {
             case 1:
             case 3:
             case 5:
             case 7:
             case 8:
             case 10:
             case 12:{
                 length=31;
                 break;
             }
             case 4:
             case 6:
             case 9:
             case 11:
             {
                 length=30;
                 break;
             }
             case 2:
             {
                 if((year%4==0&&year%100!=0)||(year%400==0))
                 {
                     length=29;break;
                 }
                 else
                 {
                     length=28;break;
                 }

             }
             }


        //String a []=new String [xinqi+30];
        for(int i=0;i<xinqi;i++)
        {
            a[i]="";
        }
        for(int i=xinqi,n=1;i<xinqi+length;i++)
        {
            a[i]=String.valueOf(n);
            n++;
        }

        for(int i=0;i<a.length;i++)
        {
           if(i%7==0&&i!=0)
           {
               System.out.println();
           }
           System.out.printf("%5s",a[i]);
           if(a[i].equals(String.valueOf(length)))
           {
               break;
           }

        }
    }
}

0

Leave a Reply

Your email address will not be published.