2022年的元旦是星期六,打印输出2022年任意月份的月历(仿电脑日历的模式,仅公历)。

5 篇文章 0 订阅
  1. Java源代码文件名:CalenderTest.java
    /**
     * @author com.whocares@183
     * @version 1.0
     */
    import java.util.Scanner;
    public class CalenderTest {
        public static void main(String[] args){
            Scanner scan = new Scanner(System.in);
            while(true) {
                System.out.println("请输入你想要查看的2022年的月份的月历:");
                int month = scan.nextInt();
                String[] arr1 = new String[]{"一", "二", "三", "四", "五", "六", "七"};
                for (String s : arr1) {
                    System.out.print(s + "\t");
                }
                int[] arr2 = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//创建一个数组来存放2022年每个月的天数
                System.out.println();
                int blanks = CalenderTest.blanks(month);//blanks方法返回需要留下几个空白,即打印几个星号
                for (int i = 0; i < blanks; i++) {//打印前面的星号数
                    System.out.print("*\t");
                }
                for (int i = 1; i <= arr2[month]; i++) {//按行打印公历日期
                    System.out.print(i + "\t");
                    blanks++;
                    if(i == arr2[month]){
                        System.out.println();
                    }
                    if (blanks % 7 == 0) {//满七个直接换行
                        System.out.println();
                    }
                }
            }
        }
        public static int blanks(int month){//返回值为要打印的星号数量
            int days = 0, blanks = 0;
            if(month == 1){
                return 5;
            }
            days += 31;//加上一月过的31天
            if(month == 2){//到2.1过了31天。
                return blanks = (days - 1) % 7 - 1;
            }
            days += 28;//加上二月过的28天
            if(month == 3){
                return blanks = (days - 1) % 7 - 1;
            }
            days += 31;//加上三月过的31天
            if(month == 4){
                return blanks = (days - 1) % 7 - 1;
            }
            days += 30;//加上四月过的30天
            if(month == 5){
                return blanks = (days - 1) % 7 - 1;
            }
            days += 31;//加上五月过的31天
            if(month == 6){
                return blanks = (days - 1) % 7 - 1;
            }
            days += 30;//加上六月过的30天
            if(month == 7){
                return blanks = (days - 1) % 7 - 1;
            }
            days += 31;//加上七月过的31天
            if(month == 8){
                return blanks = (days - 1) % 7 - 1;
            }
            days += 31;//加上八月过的31天
            if(month == 9){
                return blanks = (days - 1) % 7 - 1;
            }
            days += 30;//加上九月过的30天
            if(month == 10){
                return blanks = (days - 1) % 7 - 1;
            }days += 31;//加上十月过的31天
            if(month == 11){
                return blanks = (days - 1) % 7 - 1;
            }days += 30;//加上十一月过的30天
    //        if(month == 12){
                return blanks = (days - 1) % 7 - 1;
    //        }
        }
    }
    

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值