这是一个用JAVA做的万年历!
下面把源代码发出来和大家分享下!
同时也是想测试下代码高亮度的作用!

import java.util.Scanner;


public class Nl {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        
        System.out.println("请输入年:");
        int year = in.nextInt();
        System.out.println("请输入月:");
        int month = in.nextInt();
        
        boolean isRun = false;
        if(year%4==0&&year%100!=0||year%400==0)
        {
            isRun = true;
        }
        int yearDaySum=0; 
        for(int i=1900;i<year;i++)
        {
            if(i%4==0&&i%100!=0||i%400==0)
            {
                yearDaySum+=366;
            }else{
                yearDaySum+=365;
            }
        }
        int monthDaySum=0;
        for(int i=1;i<month;i++)
        {
            switch(i)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    monthDaySum+=31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    monthDaySum+=30;
                    break;
                case 2:
                    if(isRun)
                    {
                        monthDaySum+=29;
                    }else{
                        monthDaySum+=28;
                    }
                    break;
            }
        }
        int daySum=yearDaySum+monthDaySum;
        int week=(daySum+1)%7;

        
        int monthDay = 0;
        switch(month)
        {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            monthDay=31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            monthDay=30;
            break;
        case 2:
            if(isRun)
            {
                monthDay=29;
            }else{
                monthDay=28;
            }
            break;
        }
        
        System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
        int count=0;
        for(int i=0;i<week;i++)
        {
            System.out.print("\t");
            count++;
            
        }
        for(int i=1;i<=monthDay;i++)
        {
            System.out.print(i+"\t");
            count++;
            if(count%7==0)
            {
                System.out.println("");
            }
        }
    }
}

标签: Java, java万年历

添加新评论