java万年历课程设计

java万年历课程设计一款适用于Java课程设计的小练手项目

最近学校老师布置了课程作业,制作一个万年历,很有意思,肝了两天终于写出来了,运行情况还好,可以跑起来,这个代码适合和我一样的java初学者阅读,写的不好还请见谅

命令行窗口jar运行版本 

此代码生成的jar仅适用于window平台

import java.util.Calendar;
import java.util.Scanner;

/**
 * 日历类
 * 用于产生日历 输出日历
 * 无其他功能
 */
class Calendars {
    /**
     * 目标年份
     * :
     * 未确定查询的目标,该值为 -1
     */
    private int targetYear;
    /**
     * 目标月份
     * :
     * 未确定查询的目标,该值为 -1
     */
    private int targetMonth;
    /**
     * 目标日期
     * :
     * 未确定查询的目标,该值为 -1
     */
    private int targetDay;
    /**
     * 当前年份
     * :
     * 从系统获取当前年份
     */
    private int year;
    /**
     * 当前月份
     * :
     * 从系统获取当前月份
     */
    private int month;
    /**
     * 当前日期
     * :
     * 从系统获取当前日期
     */
    private int day;

    /**
     * 无参构造方法
     * 实现在目标构造时获取到当前日期
     * 并将目标日期置为  0
     */
    public Calendars(){
        targetDay = targetMonth = targetYear = 0;
        Calendar rightNow = Calendar.getInstance();
        year = rightNow.get(Calendar.YEAR) ;
        month = rightNow.get(Calendar.MONTH) + 1;
        day = rightNow.get(Calendar.DATE);
    }

    /**
     * 在原无参构造的基础上
     * 增加了对目标年月进行的赋值
     *
     * @param targetYear  目标年份
     * @param targetMonth 目标月份
     *
     */
    public Calendars(int targetYear,int targetMonth){
        this();
        this.targetYear = targetYear;
        this.targetMonth = targetMonth;
        this.targetDay = 1;
    }

    public int getTargetYear() {
        return targetYear;
    }

    public void setTargetYear(int targetYear) {
        this.targetYear = targetYear;
    }

    public int getTargetMonth() {
        return targetMonth;
    }

    public void setTargetMonth(int targetMonth) {
        this.targetMonth = targetMonth;
    }

    public int getTargetDay() {
        return targetDay;
    }

    public void setTargetDay(int targetDay) {
        this.targetDay = targetDay;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }


    /**
     * 对目标年份进行判断
     * 是闰年返回 true
     * 不是则返回 false
     * @param year int 要判断的年份
     * @return : true  or  false
     */
    public boolean isLeap(int year){
        //四年一闰逢百不闰四百在闰
        //通过逻辑表达式的返回值来判断是否符合条件
        boolean k = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));

        return k;
    }

    /**
     *该方法作用是返回目标年月的第一天是星期几
     * @param year int 要判断的年  要求(0,int范围]
     * @param month int 要判断的月 要求[1-12]
     * @return int [0-6]:[日-六]
     */
    public int currentWeek(int year,int month){
        //资料查询:公元元年1月1日为星期一
        int Y = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100
                + (year - 1) / 400;
        int D = 0;
        for(int i = 1;i < month; i ++){
            D += daysInMonth(year,i);
        }
        return (Y + D + 1) % 7;
    }

    /**
     *该方法的作用是返回当前月份的天数
     * @param year int 目标年份
     * @param month int 目标月份
     * @return int 当前月份天数
     */
    public int daysInMonth(int year,int month){
        int two = 28;
        if(isLeap(year)) {
            two ++;
        }
        switch (month){
            case 1: case 3:
            case 5: case 7:
            case 8: case 10:
            case 12: return 31;
            case 4: case 6:
            case 9: case 11: return 30;
            case 2:  return two;
            default: return -1;
        }
    }

    /**
     * 此方法的作用是降低代码的耦合性
     * 将日期输出语句包装成一个方法
     * @param sentence String 要输出语句
     * @param year int 要输出的年份
     * @param month int 要输出的月份
     * @param day int 要输出的日
     */
    public void print(String sentence,int year,int month,int day){
        System.out.println("|\t\t\t\t\t\t" + sentence + ":" + year + "-" + month + "-" + day + "\t\t\t\t\t\t|");
    }

    /**
     * 此方法的作用是用给定的符号s打印一行边界
     * @param s char 输出的符号
     */
    public void printSymbol(char s){
        String T = "";
        for(int i = 0;i < 111;i ++) T += s;
        System.out.println("|" + T + "|" );
    }

    /**
     * 该方法的作用是输出日历
     * @param year int 要输出日历的年
     * @param month int 要输出日历的月
     */
    public void print(int year,int month){
        int emptyDate = currentWeek(year,month);
        printSymbol('-');
        print("当前日期",this.year,this.month,this.day);
        printSymbol('-');
        if (targetYear > 0) {
            print("查询日期",this.targetYear,this.targetMonth,this.targetDay);
        }else{
            print("查询日期",this.year,this.month,this.day);
        }
        printSymbol('-');
        System.out.println("|\t一\t\t二\t\t三\t\t四\t\t五\t\t六\t\t日\t|" );
        System.out.print("|\t");
        if (emptyDate == 0) emptyDate = 7;
        for(int i = 1;i < emptyDate ;i ++){
            System.out.print("\t\t");
        }
        for(int i = 1;i <= daysInMonth(year,month);i ++){

            if(emptyDate == 7 || emptyDate == 0){
                System.out.println(i + "\t|");
                System.out.print("|\t");
            }else{
                System.out.print(i + "\t\t");
            }
            if (i == daysInMonth(year,month)){
                for(int j = 1;j < 7 - emptyDate ;j ++){
                    System.out.print("\t\t");
                }
                System.out.println("\t|");
            }
            emptyDate = (emptyDate + 1) % 7;
        }

        printSymbol('-');
    }

}

/**
 * 日历+类
 * 在日历类的基础上增加了输出查询日期节日的功能
 */
class SuperCalendar extends Calendars{
    /**
     * 存储节日名称
     */
    private final String[] festival;
    /**
     * 存储节日日期
     *
     *格式 xx_xx
     */
    private final String[] festivalDay;
    /**
     * 节日数量
     */
    private final int FEST_MAX_SIZE;

    /**
     * 对常量进行初始化
     */
    {
        FEST_MAX_SIZE = 15;
        festivalDay = new String[]{"1_1","2_14","3_8","3_12","4_1","4_5","5_1","5_4","5_8","6_1","6_19","7_1","8_1","9_10","10_1"};
        festival = new String[]{"元旦节","情人节","妇女节","植树节","愚人节","清明节","劳动节","青年节","母亲节"
                ,"儿童节","父亲节","建党节","建军节","教师节","国庆节"};
    }

    public SuperCalendar(){
        super();
    }

    public SuperCalendar(int year,int month){
        super(year,month);
    }


    public void printCalendar(int year, int month){
        super.print(year,month);
    }

    /**
     * 输出日历并输出查询日期是否存在节日
     * @param year int 要输出日历的年
     * @param month int 要输出日历的月
     */
    @Override
    public void print(int year, int month) {
        //super.print(year, month);
        printSymbol('-');
        String years = "";
        String months = "";
        String days = "";
        String st = "";
        if (year == getYear() && month == getMonth()){
            years = "" + getYear();
            months = "" + getMonth();
            days = "" + getDay();
            st = getMonth() + "_" + getDay();
        }else {
            years = "" + getTargetYear();
            months = "" + getTargetMonth();
            days = "" + getTargetDay();
            st = getTargetMonth() + "_" + getTargetDay();
        }
        while (years.length() < 4) years = "0" + years;
        while (months.length() < 2) months = "0" + months;
        while (days.length() < 2) days = "0" + days;
        System.out.println("|\t\t\t\t\t 日期\t\t" + years + "-" + months + "-" + days + "\t\t信息\t\t\t\t|");

        int i = -1;
        for(int j = 0;j < FEST_MAX_SIZE;j ++){
            if(festivalDay[j].equals(st)){
                i = j;
                break;
            }
        }
        if (i >= 0){
            System.out.println("|\t\t\t\t\t今日节日:" + "\t\t"+ festival[i] + "\t\t\t\t\t\t|");
        }else{
            System.out.println("|\t\t\t\t\t今日节日:" + "\t\t无节日\t\t\t\t\t\t|");
        }
        printSymbol('-');
    }
}

/**
 * 用于生成变量
 * 人机交互等操作放在此类
 */
class RunToSC extends SuperCalendar{

    public RunToSC(){
        super();
    }

    public void menu(){
        System.out.println("|\t\t\t\t\t\t-----<总菜单>-----\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t0.退出日历\t\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t1.打开日历\t\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t2.查询日历\t\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t3.修改日历\t\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t4.帮助文档\t\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t5.关闭日历\t\t\t\t\t\t\t|");
        printSymbol('-');
    }
    public void inquireMenu(){
        System.out.println("|\t\t\t\t\t\t----<查询菜单>----\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t  0.退出查询菜单  \t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t  1.查询今日节日  \t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t  2.查询目标节日  \t\t\t\t\t\t|");
        printSymbol('-');
    }
    public void amendMenu(){
        System.out.println("|\t\t\t\t\t\t----<修改菜单>----\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t\t0.退出修改\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t\t1.全部修改\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t\t2.修改年份\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t\t3.修改月份\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t\t4.修改日期\t\t\t\t\t\t|");
        printSymbol('-');
    }
    public void helpDocument(){
        System.out.println("|\t\t\t\t\t\t----<帮助文档>----\t\t\t\t\t\t|");
                          //|---------------------------------------|
        System.out.println("|\t\t\t\t\t1. 日历操作方法:                 \t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t      a. 根据菜单提示代号进行输入  \t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t      b. 仅需键盘操作            \t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t      c. 日历操作错误需要重新输入  \t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t2. 操作错误处理方法:             \t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t      a. 日历可以反复操作         \t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t      b. 请输入合法的代号         \t\t\t\t\t|");
        printSymbol('-');
        System.out.println("输入任意数字按回车退出...");
    }

    public void setYear(Scanner input){
        System.out.println("请输入带查询年份:");
        int years = input.nextInt();
        if(years > 0) {
            setTargetYear(years);
        }else{
            System.out.println("请输入正确年份...\n按任意数字回车继续...");
            years = input.nextInt();
            setYear(input);
        }
    }

    public void setMonth(Scanner input){
        System.out.println("请输入带查询月份:");
        int months = input.nextInt();
        if(months > 0 && months <= 12) {
            setTargetMonth(months);
        }else{
            System.out.println("请输入正确月份...\n按任意数字回车继续...");
            months = input.nextInt();
            setMonth(input);
        }
    }

    public void setDay(Scanner input){
        System.out.println("请输入带查询日期:");
        int days = input.nextInt();
        int maxDay = daysInMonth(getTargetYear(),getTargetMonth());
        if(days > 0 && days <= maxDay) {
            setTargetDay(days);
        }else{
            System.out.println("请输入正确日期...\n按任意数字回车继续...");
            days = input.nextInt();
            setDay(input);
        }
    }

    public void clear(){
        //网上扒的代码***
        try {//使用命令的过程可能会出现失败,需要捕获异常
            //   Process process = Runtime.getRuntime().exec("cls");
            new ProcessBuilder("cmd", "/c", "cls")
                    // 将 ProcessBuilder 对象的输出管道和 Java 的进程进行关联,这个函数的返回值也是一个
                    // ProcessBuilder
                    .inheritIO()
                    // 开始执行 ProcessBuilder 中的命令
                    .start()
                    // 等待 ProcessBuilder 中的清屏命令执行完毕
                    // 如果不等待则会出现清屏代码后面的输出被清掉的情况
                    .waitFor(); // 清屏命令
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run(){
        Scanner input = new Scanner(System.in);
        if (getTargetDay() == 0){
            setTargetYear(getYear());
            setTargetMonth(getMonth());
            setTargetDay(getDay());
        }
        int judgeOverall = 1;
        int judgeAmend = -1;
        int judgeInquire = -1;
        int judgeHelp = -1;
        int tmp = 0;
        boolean outCalender = false;
        while (judgeOverall != 0){
            print(getYear(),getMonth());
            if (outCalender){
                printCalendar(getTargetYear(),getTargetMonth());
            }
            menu();
            judgeOverall = input.nextInt();
            switch (judgeOverall){
                case 0:
                    //退出日历
                    break;
                case 1: {
                    //打开日历
                    outCalender = true;
                }break;
                case 2:{
                    //查询日历
                    clear();
                    print(getYear(),getMonth());
                    inquireMenu();
                    judgeInquire = input.nextInt();
                    switch (judgeInquire){
                        case 0:{
                            judgeInquire = 0;
                        }break;
                        case 1:{
                            clear();
                            print(getYear(),getMonth());
                            printCalendar(getYear(),getMonth());
                            System.out.println("按任意数字回车退出...");
                            tmp = input.nextInt();
                        }break;
                        case 2:{
                            clear();
                            print(getTargetYear(),getTargetMonth());
                            printCalendar(getTargetYear(),getTargetMonth());
                            System.out.println("按任意数字回车退出...");
                            tmp = input.nextInt();
                        }break;
                        default:
                    }
                }break;
                case 3:{
                    //修改日历
                    clear();
                    print(getYear(),getMonth());
                    amendMenu();
                    judgeAmend = input.nextInt();
                    switch (judgeAmend){
                        case 0:{
                            judgeAmend = 0;
                        }break;
                        case 1:{
                            setYear(input);
                            setMonth(input);
                            setDay(input);
                        }break;
                        case 2:{
                            setYear(input);
                        }break;
                        case 3:{
                            setMonth(input);
                        }break;
                        case 4:{
                            setDay(input);
                        }break;
                        default:
                    }
                }break;
                case 4:{
                    //帮助文档
                    clear();
                    print(getYear(),getMonth());
                    helpDocument();
                    judgeHelp = input.nextInt();
                }break;
                case 5:{
                    //关闭日历
                    outCalender = false;
                }break;
                default:
            }
            clear();
        }
    }
}

该代码在idea运行结果会出现格式错误

java万年历课程设计

 生成jar后在命令行窗口运行

java万年历课程设计

 idea运行版本

该版本在命令行窗口运行会出现格式错误

import java.util.Calendar;
import java.util.Scanner;

/**
 * 日历类
 * 用于产生日历 输出日历
 * 无其他功能
 */
class Calendars {
    /**
     * 目标年份
     * :
     * 未确定查询的目标,该值为 -1
     */
    private int targetYear;
    /**
     * 目标月份
     * :
     * 未确定查询的目标,该值为 -1
     */
    private int targetMonth;
    /**
     * 目标日期
     * :
     * 未确定查询的目标,该值为 -1
     */
    private int targetDay;
    /**
     * 当前年份
     * :
     * 从系统获取当前年份
     */
    private int year;
    /**
     * 当前月份
     * :
     * 从系统获取当前月份
     */
    private int month;
    /**
     * 当前日期
     * :
     * 从系统获取当前日期
     */
    private int day;

    /**
     * 无参构造方法
     * 实现在目标构造时获取到当前日期
     * 并将目标日期置为  0
     */
    public Calendars(){
        targetDay = targetMonth = targetYear = 0;
        Calendar rightNow = Calendar.getInstance();
        year = rightNow.get(Calendar.YEAR) ;
        month = rightNow.get(Calendar.MONTH) + 1;
        day = rightNow.get(Calendar.DATE);
    }

    /**
     * 在原无参构造的基础上
     * 增加了对目标年月进行的赋值
     *
     * @param targetYear  目标年份
     * @param targetMonth 目标月份
     *
     */
    public Calendars(int targetYear,int targetMonth){
        this();
        this.targetYear = targetYear;
        this.targetMonth = targetMonth;
        this.targetDay = 1;
    }

    public int getTargetYear() {
        return targetYear;
    }

    public void setTargetYear(int targetYear) {
        this.targetYear = targetYear;
    }

    public int getTargetMonth() {
        return targetMonth;
    }

    public void setTargetMonth(int targetMonth) {
        this.targetMonth = targetMonth;
    }

    public int getTargetDay() {
        return targetDay;
    }

    public void setTargetDay(int targetDay) {
        this.targetDay = targetDay;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }


    /**
     * 对目标年份进行判断
     * 是闰年返回 true
     * 不是则返回 false
     * @param year int 要判断的年份
     * @return : true  or  false
     */
    public boolean isLeap(int year){
        //四年一闰逢百不闰四百在闰
        //通过逻辑表达式的返回值来判断是否符合条件
        boolean k = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));

        return k;
    }

    /**
     *该方法作用是返回目标年月的第一天是星期几
     * @param year int 要判断的年  要求(0,int范围]
     * @param month int 要判断的月 要求[1-12]
     * @return int [0-6]:[日-六]
     */
    public int currentWeek(int year,int month){
        //资料查询:公元元年1月1日为星期一
        int Y = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100
                + (year - 1) / 400;
        int D = 0;
        for(int i = 1;i < month; i ++){
            D += daysInMonth(year,i);
        }
        return (Y + D + 1) % 7;
    }

    /**
     *该方法的作用是返回当前月份的天数
     * @param year int 目标年份
     * @param month int 目标月份
     * @return int 当前月份天数
     */
    public int daysInMonth(int year,int month){
        int two = 28;
        if(isLeap(year)) {
            two ++;
        }
        switch (month){
            case 1: case 3:
            case 5: case 7:
            case 8: case 10:
            case 12: return 31;
            case 4: case 6:
            case 9: case 11: return 30;
            case 2:  return two;
            default: return -1;
        }
    }

    /**
     * 此方法的作用是降低代码的耦合性
     * 将日期输出语句包装成一个方法
     * @param sentence String 要输出语句
     * @param year int 要输出的年份
     * @param month int 要输出的月份
     * @param day int 要输出的日
     */
    public void print(String sentence,int year,int month,int day){
        System.out.println("|\t\t\t\t\t" + sentence + ":" + year + "-" + month + "-" + day + "\t\t\t\t\t|");
    }

    /**
     * 此方法的作用是用给定的符号s打印一行边界
     * @param s char 输出的符号
     */
    public void printSymbol(char s){
        String T = "";
        for(int i = 0;i < 55;i ++) T += s;
        System.out.println("|" + T + "|" );
    }

    /**
     * 该方法的作用是输出日历
     * @param year int 要输出日历的年
     * @param month int 要输出日历的月
     */
    public void print(int year,int month){
        int emptyDate = currentWeek(year,month);
        printSymbol('-');
        print("当前日期",this.year,this.month,this.day);
        printSymbol('-');
        if (targetYear > 0) {
            print("查询日期",this.targetYear,this.targetMonth,this.targetDay);
        }else{
            print("查询日期",this.year,this.month,this.day);
        }
        printSymbol('-');
        System.out.println("|\t一\t\t二\t\t三\t\t四\t\t五\t\t六\t\t日\t|" );
        System.out.print("|\t");
        for(int i = 1;i < emptyDate ;i ++){
            System.out.print("\t\t");
        }
        for(int i = 1;i <= daysInMonth(year,month);i ++){

            if(emptyDate == 7 || emptyDate == 0){
                System.out.println(i + "\t|");
                System.out.print("|\t");
            }else{
                System.out.print(i + "\t\t");
            }
            if (i == daysInMonth(year,month)){
                for(int j = 1;j < 7 - emptyDate ;j ++){
                    System.out.print("\t\t");
                }
                System.out.println("\t|");
            }
            emptyDate = (emptyDate + 1) % 7;
        }

        printSymbol('-');
    }

}

/**
 * 日历+类
 * 在日历类的基础上增加了输出查询日期节日的功能
 */
class SuperCalendar extends Calendars{
    /**
     * 存储节日名称
     */
    private final String[] festival;
    /**
     * 存储节日日期
     *
     *格式 xx_xx
     */
    private final String[] festivalDay;
    /**
     * 节日数量
     */
    private final int FEST_MAX_SIZE;

    /**
     * 对常量进行初始化
     */
    {
        FEST_MAX_SIZE = 15;
        festivalDay = new String[]{"1_1","2_14","3_8","3_12","4_1","4_5","5_1","5_4","5_8","6_1","6_19","7_1","8_1","9_10","10_1"};
        festival = new String[]{"元旦节","情人节","妇女节","植树节","愚人节","清明节","劳动节","青年节","母亲节"
                ,"儿童节","父亲节","建党节","建军节","教师节","国庆节"};
    }

    public SuperCalendar(){
        super();
    }

    public SuperCalendar(int year,int month){
        super(year,month);
    }


    public void printCalendar(int year, int month){
        super.print(year,month);
    }

    /**
     * 输出日历并输出查询日期是否存在节日
     * @param year int 要输出日历的年
     * @param month int 要输出日历的月
     */
    @Override
    public void print(int year, int month) {
        //super.print(year, month);
        printSymbol('-');
        String years = "";
        String months = "";
        String days = "";
        if (year == getYear() && month == getMonth()){
            years = "" + getYear();
            months = "" + getMonth();
            days = "" + getDay();
        }else {
            years = "" + getTargetYear();
            months = "" + getTargetMonth();
            days = "" + getTargetDay();
        }
        while (years.length() < 4) years = "0" + years;
        while (months.length() < 2) months = "0" + months;
        while (days.length() < 2) days = "0" + days;
        System.out.println("|\t\t\t\t日期\t\t" + years + "-" + months + "-" + days + "\t\t信息\t\t\t\t|");
        String st = getTargetMonth() + "_" + getTargetDay();
        int i = -1;
        for(int j = 0;j < FEST_MAX_SIZE;j ++){
            if(festivalDay[j].equals(st)){
                i = j;
                break;
            }
        }
        if (i >= 0){
            System.out.println("|\t\t\t\t\t今日节日:" + "\t\t"+ festival[i] + "\t\t\t\t|");
        }else{
            System.out.println("|\t\t\t\t\t今日节日:" + "\t\t无节日\t\t\t\t|");
        }
        printSymbol('-');
    }
}

/**
 * 用于生成变量
 * 人机交互等操作放在此类
 */
class RunToSC extends SuperCalendar{

    public RunToSC(){
        super();
    }

    public void menu(){
        System.out.println("|\t\t\t\t\t-----<总菜单>-----\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t0.退出日历\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t1.打开日历\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t2.查询日历\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t3.修改日历\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t4.帮助文档\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t5.关闭日历\t\t\t\t\t\t|");
        printSymbol('-');
    }
    public void inquireMenu(){
        System.out.println("|\t\t\t\t\t----<查询菜单>----\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t  0.退出查询菜单  \t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t  1.查询今日节日  \t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t  2.查询目标节日  \t\t\t\t\t|");
        printSymbol('-');
    }
    public void amendMenu(){
        System.out.println("|\t\t\t\t\t----<修改菜单>----\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t0.退出修改\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t1.全部修改\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t2.修改年份\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t3.修改月份\t\t\t\t\t\t|");
        System.out.println("|\t\t\t\t\t\t4.修改日期\t\t\t\t\t\t|");
        printSymbol('-');
    }
    public void helpDocument(){
        System.out.println("|\t\t\t\t\t----<帮助文档>----\t\t\t\t\t|");
                          //|---------------------------------------|
        System.out.println("|\t\t\t1. 日历操作方法:                 \t\t\t\t|");
        System.out.println("|\t\t\t      a. 根据菜单提示代号进行输入  \t\t\t\t|");
        System.out.println("|\t\t\t      b. 仅需键盘操作            \t\t\t\t|");
        System.out.println("|\t\t\t      c. 日历操作错误需要重新输入  \t\t\t\t|");
        System.out.println("|\t\t\t2. 操作错误处理方法:             \t\t\t\t|");
        System.out.println("|\t\t\t      a. 日历可以反复操作         \t\t\t\t|");
        System.out.println("|\t\t\t      b. 请输入合法的代号         \t\t\t\t|");
        printSymbol('-');
        System.out.println("输入任意数字按回车退出...");
    }

    public void setYear(Scanner input){
        System.out.println("请输入带查询年份:");
        int years = input.nextInt();
        if(years > 0) {
            setTargetYear(years);
        }else{
            System.out.println("请输入正确年份...\n按任意数字回车继续...");
            years = input.nextInt();
            setYear(input);
        }
    }

    public void setMonth(Scanner input){
        System.out.println("请输入带查询月份:");
        int months = input.nextInt();
        if(months > 0 && months <= 12) {
            setTargetMonth(months);
        }else{
            System.out.println("请输入正确月份...\n按任意数字回车继续...");
            months = input.nextInt();
            setMonth(input);
        }
    }

    public void setDay(Scanner input){
        System.out.println("请输入带查询日期:");
        int days = input.nextInt();
        int maxDay = daysInMonth(getTargetYear(),getTargetMonth());
        if(days > 0 && days <= maxDay) {
            setTargetDay(days);
        }else{
            System.out.println("请输入正确日期...\n按任意数字回车继续...");
            days = input.nextInt();
            setDay(input);
        }
    }

    public void clear(){
        //网上扒的代码***
        try {//使用命令的过程可能会出现失败,需要捕获异常
            //   Process process = Runtime.getRuntime().exec("cls");
            new ProcessBuilder("cmd", "/c", "cls")
                    // 将 ProcessBuilder 对象的输出管道和 Java 的进程进行关联,这个函数的返回值也是一个
                    // ProcessBuilder
                    .inheritIO()
                    // 开始执行 ProcessBuilder 中的命令
                    .start()
                    // 等待 ProcessBuilder 中的清屏命令执行完毕
                    // 如果不等待则会出现清屏代码后面的输出被清掉的情况
                    .waitFor(); // 清屏命令
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run(){
        Scanner input = new Scanner(System.in);
        if (getTargetDay() == 0){
            setTargetYear(getYear());
            setTargetMonth(getMonth());
            setTargetDay(getDay());
        }
        int judgeOverall = 1;
        int judgeAmend = -1;
        int judgeInquire = -1;
        int judgeHelp = -1;
        int tmp = 0;
        boolean outCalender = false;
        while (judgeOverall != 0){
            print(getYear(),getMonth());
            if (outCalender){
                printCalendar(getTargetYear(),getTargetMonth());
            }
            menu();
            judgeOverall = input.nextInt();
            switch (judgeOverall){
                case 0:
                    //退出日历
                    break;
                case 1: {
                    //打开日历
                    outCalender = true;
                }break;
                case 2:{
                    //查询日历
                    clear();
                    print(getYear(),getMonth());
                    inquireMenu();
                    judgeInquire = input.nextInt();
                    switch (judgeInquire){
                        case 0:{
                            judgeInquire = 0;
                        }break;
                        case 1:{
                            clear();
                            print(getYear(),getMonth());
                            printCalendar(getYear(),getMonth());
                            System.out.println("按任意数字回车退出...");
                            tmp = input.nextInt();
                        }break;
                        case 2:{
                            clear();
                            print(getTargetYear(),getTargetMonth());
                            printCalendar(getTargetYear(),getTargetMonth());
                            System.out.println("按任意数字回车退出...");
                            tmp = input.nextInt();
                        }break;
                        default:
                    }
                }break;
                case 3:{
                    //修改日历
                    clear();
                    print(getYear(),getMonth());
                    amendMenu();
                    judgeAmend = input.nextInt();
                    switch (judgeAmend){
                        case 0:{
                            judgeAmend = 0;
                        }break;
                        case 1:{
                            setYear(input);
                            setMonth(input);
                            setDay(input);
                        }break;
                        case 2:{
                            setYear(input);
                        }break;
                        case 3:{
                            setMonth(input);
                        }break;
                        case 4:{
                            setDay(input);
                        }break;
                        default:
                    }
                }break;
                case 4:{
                    //帮助文档
                    clear();
                    print(getYear(),getMonth());
                    helpDocument();
                    judgeHelp = input.nextInt();
                }break;
                case 5:{
                    //关闭日历
                    outCalender = false;
                }break;
                default:
            }
            clear();
        }
    }
}

结果:

java万年历课程设计

运行提示:定义RunToCS对象,调用run函数即可运行

今天的文章java万年历课程设计分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/30864.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注