根据年份输出日历

来源:互联网 发布:stc15l2k08s2数据手册 编辑:程序博客网 时间:2024/04/29 01:08

这里写图片描述
代码如下:

public class Main {    public static void main(String[] args) {        // write your code here        rili();    }    public static void rili() {        Scanner scanner = new Scanner(System.in);        int sum = 0;        System.out.print("请输入年份:");        int year = scanner.nextInt();        int month = 1;        for (int i = 1900; i < year; i++) {            if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {                sum += 366;            } else {                sum += 365;            }        }        sum += 1;        while (month <= 12) {            int days;            if (month == 2) {                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {                    days = 29;                } else {                    days = 28;                }            } else if (month == 4 || month == 6 || month == 9 || month == 11) {                days = 30;            } else {                days = 31;            }            int weekdays = sum % 7 + 1;            System.out.println("==========第" + month + "月==========");            System.out.println("日\t一\t二\t三\t四\t五\t六");            for (int i = 1; i < weekdays; i++) {                System.out.print("\t");            }            for (int i = 1; i <= days; i++) {                if (sum % 7 == 6) {                    System.out.println(i);                } else {                    System.out.print(i + "\t");                }                sum += 1;            }            System.out.println();            month++;        }    }}
0 0
原创粉丝点击