其他分享
首页 > 其他分享> > 输入年月日,各种判断年份(平润年)来输出这是多少年的第几天

输入年月日,各种判断年份(平润年)来输出这是多少年的第几天

作者:互联网

通过输入年月日,各种判断年份(平润年)来输出这是多少年的第几天

用switch方法1:

const readline = require('readline-sync');
console.log('请输入一个年份');
let year = readline.question();
let day, count, month;
if(year>=1&&year<=9999){
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
        console.log('这是闰年,请输入一个月份');
        month = readline.question() - 0;
        switch (month) {
            case 1:
                console.log('请输入天数');
                day = readline.question() - 0;
                if (day >= 1 && day <= 31) {
                    count = parseInt(month / 2);
                    console.log(`这是这一年的第${day}天`);
                } else {
                    console.log('输入的天数不合法');
                }
                break;
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                console.log('请输入天数');
                day = readline.question() - 0;
                if (day >= 1 && day <= 31) {
                    count = parseInt(month / 2);
                    if (count <= 3) {
                        console.log(`这是这一年的第${(((count * 31) + (count * 30)) - 1) + day}天`);
                    } else {
                        console.log(`这是这一年的第${((((count - 1) * 30) + (count * 31)) - 1) + day}天`);
                    }
                } else {
                    console.log('输入的天数不合法');
                }
                break;
            case 2:
                console.log('请输入天数');
                day = readline.question() - 0;
                if (day >= 1 && day <= 29) {
                    console.log(`这是这一年的第${31 + day}天`);
                } else {
                    console.log('输入的天数不合法');
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                console.log('请输入天数');
                day = readline.question() - 0;
                if (day >= 1 && day <= 30) {
                    count = parseInt(month / 2);
                    if (count <= 3) {
                        console.log(`这是这一年的第${(((count * 31) + ((count - 1) * 30)) - 1) + day}天`);
                    } else {
                        console.log(`这是这一年的第${((((count - 1) * 30) + ((count + 1) * 31)) - 1) + day}天`);
                    }
                } else {
                    console.log('输入的天数不合法');
                }
                break;
            default:
                console.log('输入的月份不合理');
        }
    
    } else {
        console.log('这是平年,请输入一个月份');
        month = readline.question() - 0;
        switch (month) {
            case 1:
                console.log('请输入天数');
                day = readline.question() - 0;
                if (day >= 1 && day <= 31) {
                    count = parseInt(month / 2);
                    console.log(`这是这一年的第${day}天`);
                } else {
                    console.log('输入的天数不合法');
                }
                break;
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                console.log('请输入天数');
                day = readline.question() - 0;
                if (day >= 1 && day <= 31) {
                    count = parseInt(month / 2);
                    if (count <= 3) {
                        console.log(`这是这一年的第${(((count * 31) + (count * 30)) - 2) + day}天`);
                    } else {
                        console.log(`这是这一年的第${((((count - 1) * 30) + (count * 31)) - 2) + day}天`);
                    }
                } else {
                    console.log('输入的天数不合法');
                }
                break;
            case 2:
                console.log('请输入天数');
                day = readline.question() - 0;
                if (day >= 1 && day <= 28) {
                    console.log(`这是这一年的第${31 + day}天`);
                } else {
                    console.log('输入的天数不合法');
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                console.log('请输入天数');
                day = readline.question() - 0;
                if (day >= 1 && day <= 30) {
                    count = parseInt(month / 2);
                    if (count <= 3) {
                        console.log(`这是这一年的第${(((count * 31) + ((count - 1) * 30)) - 2) + day}天`);
                    } else {
                        console.log(`这是这一年的第${((((count - 1) * 30) + ((count + 1) * 31)) - 2) + day}天`);
                    }
                } else {
                    console.log('输入的天数不合法');
                }
                break;
            default:
                console.log('输入的月份不合理');
    
        }
    }
}
else{
    console.log('输入的年份不合理');
}

用if方法2:

const readline = require('readline-sync')

let year, month, day;
let isRN = false;//控制是否是闰年
let flag = false;//控制是否输入的日期都正确
console.log('请输入年份');
year = readline.question() - 0;
if (year >= 1 && year <= 9999) {
    //判断是否是闰年
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        //是闰年
        isRN = true;
    }
    //判断月份是否正确
    console.log('请输入月份');
    month = readline.question() - 0;
    if (month >= 1 && month <= 12) {
        //判断天数是否正确
        console.log('请输入天数');
        day = readline.question() - 0;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            //1、3、5、7、8、10、12 
            if (day >= 1 && day <= 31) {
                console.log('所有日期全部输入正确');
                flag = true;
            } else {
                console.log('输入的天数不正确');
            }

        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            //4、6、9、11
            if (day >= 1 && day <= 30) {
                console.log('所有日期全部输入正确');
                flag = true;
            } else {
                console.log('输入的天数不正确');
            }
        } else if (month == 2) {
            //2
            if (isRN) {
                if (day >= 1 && day <= 29) {
                    console.log('所有日期全部输入正确');
                    flag = true;
                } else {
                    console.log('输入的天数不正确');
                }
            } else {
                if (day >= 1 && day <= 28) {
                    console.log('所有日期全部输入正确');
                    flag = true;
                } else {
                    console.log('输入的天数不正确');
                }
            }
        }
    } else {
        console.log('月份输入不合法');
    }



} else {
    console.log('输入的年份不正确');
}

if (flag) {
    let count = 0;
    let m1 = 31, m2 = 28, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30, m12 = 31;
    switch (month) {
        case 1:
            count = day;
            break;
        case 2:
            count = m1 + day;
            break;
        case 3:
            count = m1 + m2 + day;
            break;
        case 4:
            count = m1 + m2 + m3 + day;
            break;
        case 5:
            count = m1 + m2 + m3 + m4 + day;
            break;
        case 6:
            count = m1 + m2 + m3 + m4 + m5 + day;
            break;
    }

    if (isRN && month > 2) {
        count += 1;
    }
    console.log(`${year}年${month}月${day}日是这一年的第${count}天`);
}

标签:count,case,console,log,第几天,day,年月日,month,平润
来源: https://blog.csdn.net/m0_62008199/article/details/122771934