打印万年历表

/*
制作万历表
分析:
1、获取年份和月份的数据
2、获取上个月到1900年1月1日的总天数
3、然后除以7取余数,余数就是1号前的空格数
 */
import java.util.Scanner;
public class Exercise8 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个年份:");
        int a = input.nextInt();
        System.out.print("请输入一个月份:");
        int b = input.nextInt();
        int c = 0;          //计算总天数
        int d1 = 0;          //计算
        for (int i = 1900; i < a; i++) {//判断年份

            if (i%4 == 0 && i%100 !=0 || i%400 == 0){
                c = c+366;
            }else
                c = c+365;
        }
        for (int i = 1; i <= b; i++) {//判断月份
            switch(i){
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                        c += 31;
                    break;
                case 2:
                    if (a%4 == 0 && a%100 !=0 || a%400 == 0){
                        c += 29;
                        break;
                    }else{
                        c += 28;
                        break;
                    }
                case 4:
                case 6:
                case 9:
                case 11:
                    c +=30;
                    break;
            }
        }
        int dum = sele(a,b);
        int c1 = c - dum;
        System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日\t");
        for (int i = 0; i < c1%7; i++) {
            System.out.print(" \t\t");
            d1++;
        }
        for (int i = 1; i <=dum ; i++) {
            System.out.print(i+"\t\t");
            d1++;
            if (d1%7 == 0)
                System.out.println();
        }
        System.out.println();
        System.out.println(c);
    }
    public static int sele(int a,int b){
        int d = 0;
        switch(b){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                d = 31;
                break;
            case 2:
                if (a%4 == 0 && a%100 !=0 || a%400 == 0){
                    d = 29;
                    break;
                }else{
                    d = 28;
                    break;
                }
            case 4:
            case 6:
            case 9:
            case 11:
                d = 30;
                break;
        }
        return d;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值