java做万年历

1.判断输入的年份是否为闰年

2.判断输入月份的天数

3.判断输入月份的第一天为星期几 

package com.yc;
import java.util.*;
public class calendar {
    public static void main(String []args) {
        int year=0,month=0;//定义年份和月份
        boolean yn=false;
        int monthofday=0;//定义每个月的天数
        int monthday=0,yearday=0,allday=0;
        int firstweek=0;
        Scanner sc=new Scanner(System.in);//数据键盘输入
        System.out.println("**************************欢迎使用万年历*******************");
        System.out.println("请输入年份:");
        if(sc.hasNextInt()) {//判断键盘输入的数据类型是否出错
            year=sc.nextInt();
            if(year<1900||year>9999) {
                System.out.println("年份输入错误,请从新输入:");
                if(sc.hasNextInt()) {
                    year=sc.nextInt();
                }else {
                    System.out.println("年份输入错误!");
                    System.exit(0);//直接跳出系统
                }
            }
        }else {
            System.out.println("年份输入错误!");
            System.exit(0);//直接跳出系统
        }
        System.out.println("请输入月份:");
        if(sc.hasNextInt()) {
            month=sc.nextInt();
            if(month<1||month>12) {
                System.out.println("月份输入错误,请从新输入:");
                if(sc.hasNextInt()) {
                    month=sc.nextInt();
                }else {
                    System.out.println("月份输入错误!");
                    System.exit(0);//直接跳出系统
                }
            }
        }else {
            System.out.println("月份输入错误!");
            System.exit(0);//直接跳出系统
        }
        if(year%4==0&&year%100!=0||year%400==0)//判断是否是闰年
            yn=true;
        switch(month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:monthofday=31;break;
        case 4:
        case 6:
        case 9:
        case 11:monthofday=30;break;
        case 2:if(yn)
                    monthofday=29;
               else
                    monthofday=28;
        
        }
        //计算1900~当前年份之前所有的天数
        for( int y=1900;y<year;y++)
            yearday+=y%4==0&&y%100!=0||y%400==0? 366:365;
        for(int d=1;d<month;d++)
            switch(d) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:monthday+=31;break;
            case 4:
            case 6:
            case 9:
            case 11:monthday+=30;break;
            case 2:if(yn)
                        monthday+=29;
                   else
                        monthday+=28;
            
            }
        System.out.println(yn);//利用Boolean类型做标签,输出Boolean类型是否正确,可删除该语句
        allday=monthday+yearday;
        System.out.println(allday);//输出1900到上月的总天数
        firstweek=allday%7+1;//计算输入的日期是这个星期的第几天
        System.out.println("这个月的第一天是礼拜"+firstweek);
        System.out.println("\n\n星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期天\t");
        //打印1号之前的空格
        int w=1;
        while(w<firstweek) {
            System.out.print("\t");
            w++;
        }//通过上面判断的fristweek计算要打印几个空格,w=7
        for(int d=1;d<=monthofday;d++,w++) {
            if(w==8) {
                w=1;
                System.out.println();
            }
            System.out.print(d+"\t");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值