编程语言
首页 > 编程语言> > Java打印日历

Java打印日历

作者:互联网

描述

已知2020年第一天是星期日 ,打印2020年某月的月历。

输入

输入月份

输出

输出提示:
System.out.printf("%4s%4s%4s%4s%4s%4s%4s","日","一","二","三","四","五","六");
System.out.println("*****2020年"+month+"月份*****");
System.out.printf("%3d",day);

难度

入门

输入示例

2

输出示例

*****2020年2月份*****
日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int month=in.nextInt();
        print(month);
    }
    public static int day(int i){
        int k=-1;
        switch (i){
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:k=31;break;
            case 4: case 6: case 9: case 11:k=30;break;
            case 2:k=29;break;
            default:break;
        }
        return k;
    }
    public static void print(int month){
        int sum=3;
        int k=day(month);
        for (int j = 1; j < month; j++)
            sum+=day(j);
        int s=sum%7;
        System.out.println("*****2020年"+month+"月份*****");
        System.out.printf("%4s%4s%4s%4s%4s%4s%4s","日","一","二","三","四","五","六");
        System.out.println();
        int n=1;
        for (int i = 0; i < s; i++)
            System.out.printf("%3s"," ");
        for (;s<7;s++){
            System.out.printf("%3d",n);
            n++;
        }
        System.out.println();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.printf("%3d",n);
                n++;
                if (n>k)break;
            }
            System.out.println();
        }

    }
}

 

 

标签:case,日历,Java,int,打印,System,month,4s%,out
来源: https://www.cnblogs.com/Blogwjl/p/12346825.html