最近在看java。之前学程序的时候没得选,宁波当地只有PHP,现在因为公司有这个需求,所以。。。。

从北京回来以后,一直忙一个接盘项目,18号刚刚结束。边看录像边学,有点基础的学起来不是很费劲。

PHP和Java写起来感觉还是不一样的,至少在类型转换和单双引号,不像PHP那么随意了,注意点代码规范就行了。

输入2个值,一个年份,一个月份。简单的if嵌套,效果图在下面。(写这种程序,感觉回到了刚刚学PHP的时候 -_-|||)

QQ截图20190420152432.jpg

import java.util.Scanner;
public class date
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int year;
        int month;
        System.out.println("*********万年历*********");
        System.out.println("请输入年份:");
        year = in.nextInt();
        System.out.println("请输入月份:");
        month = in.nextInt();
        if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
        {
            System.out.println("闰年");
        }
        else
        {
            System.out.println("平年");
        }
        if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            System.out.println("31天");
        }
        else if(month == 4 || month == 6 || month == 9 || month == 11)
        {
            System.out.println("30天");
        }
        else if(month == 2)
        {
            if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
            {
                System.out.println("29天");
            }
            else
            {
                System.out.println("28天");
            }
        }
        else
        {
            System.out.println("月份有误");
        }
    }
}

2019.4.22

又加入新的代码 显示当月 星期和日期

QQ截图20190422161336.jpg

import java.util.Scanner;
public class date
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int year;
        int month;
        int day = 0;
        int forday = 0;
        int week = 0;
        System.out.println("************************万年历************************");
        System.out.print("请输入年份:");
        year = in.nextInt();
        System.out.print("请输入月份:");
        month = in.nextInt();
        if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
        {
            System.out.println("闰年");
        }
        else
        {
            System.out.println("平年");
        }
        if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            System.out.println("31天");
        }
        else if(month == 4 || month == 6 || month == 9 || month == 11)
        {
            System.out.println("30天");
        }
        else if(month == 2)
        {
            if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
            {
                System.out.println("29天");
            }
            else
            {
                System.out.println("28天");
            }
        }
        else
        {
            System.out.println("月份有误");
        }
        System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
        for (int i = 1900;i <= year; i++) 
        {
            if((i % 400 == 0) || (i % 4 == 0 && i % 100 != 0))
            {
                day += 366;
            }
            else
            {
                day += 365;
            }
        }
        for (int i = 1;i < month; i++) 
        {
            if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
            {
                day += 31;
            }
            else if(i == 4 || i == 6 || i == 9 || i == 11)
            {
                day += 30;
            }
            else if(i == 2)
            {
                if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                {
                    day += 29;
                }
                else
                {
                    day += 28;
                }
            }
        }
        week = day % 7;
        if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            forday += 31;
        }
        else if(month == 4 || month == 6 || month == 9 || month == 11)
        {
            forday += 30;
        }
        else if(month == 2)
        {
            if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
            {
                forday += 29;
            }
            else
            {
                forday += 28;
            }
        }
        for (int i = 0; i < week; i++) 
        {
            System.out.print("\t");
        }
        for (int i = 1; i <= forday; i++) 
        {
            System.out.print(i + "\t");
            if((week + i) % 7 == 0)
            {
                System.out.print("\n");
            }
        }
    }
}