java万年历小程序

  之前学习C语言的时候编写过一次,现在学习java,基本思路和语法并没有变,所以再编一次熟悉下语法,这里只用了一个class文件,其实还可以把日历封装成一个类,类中方法私有化,作为工具只提供一个接收年份的入口。

java万年历小程序

import java.util.Scanner;

 

class  RiLi

{

             public static void main(String[] args)

             {

                Scanner input = new Scanner(System.in);

                System.out.println("请输入随意一个年份!:");

                int year = input.nextInt();

 

                printRiLi(year);

 

             }

             //判断是否闰年,是闰年就返回1,否则返回0

             public static int isRunYear(int year)

             {

                if((year%4==0 && year0!=0) || (year0==0 && year@0==0))

                    return 1;

                return 0;

             }

             //传入目标年x,计算该年第一天是星期几

             public static int getFirstDay(int year)

             {

                int runNumber = 0;

                int pingNumber = 0;

                int firstDay;

                for(int i=1900; i

                {

                    if(isRunYear(i)==1)

                        runNumber++;

                    else

                        pingNumber++;

                }

                firstDay = (366*runNumber+365*pingNumber+1)%7;//目标年第一天星期数

                return firstDay;

             }

             //获取某月份天数,传入月份和该年份

             public static int days_Month(int month,int year)

             {

                switch(month)

                {

                    case 1:

                    case 3:

                    case 5:

                    case 7:

                    case 8:

                    case 10:

                    case 12:

                        return 31;

                    case 2:

                        if(isRunYear(year)==1)

                            return 29;

                        else

                            return 28;

                    default:

                        return 30;

                }

             }

             //输出日历

             public static void printRiLi(int year)

             {

                int month;

                int days_Month;

                int firstDay;

 

                firstDay = getFirstDay(year);//获取该年第一天星期几(0~6)

 

                for (month=1; month<=12; month++)//按月打印输出

                {

                    System.out.println("            "+month+"                   ");

                    System.out.println("---------------------------------------------------");

                    System.out.println("SUN MON TUE WES THU FRI SAT");

                    System.out.println("---------------------------------------------------");

                    days_Month = days_Month(month,year);//获取本月分天数

                    //System.out.println(firstDay);

                    for (int j=0; j根据firstDay进行对齐操作

                    {

                      System.out.print("    ");

                    }

                    for (int i=1; i<=days_Month; i++)

                    {

                        System.out.print(" "+i+"\t");

                        firstDay++;

                        if(firstDay%7==0)

                            System.out.println();

                    }

                    firstDay = firstDay%7;//计算下一个月的第一天星期几

                    System.out.println();

                }

             }

 

}

  其中的计算某年的第一天的算法并不固定,而且程序以1900年为基准年,所以1900年以前的日历应该是不对的哈。

你可能感兴趣的:(java学习)