用JS寫,根據用戶輸入的年月份判斷是這年的第幾天


console.log("輸入年份:");
let year = readline.question() - 0;
console.log("輸入一個月份");
let month = readline.question() - 0;
console.log("輸入天數");
let day = readline.question() - 0;

if (year > 999 && year < 10000) {

    if (month > 0 && month < 13) {

        if (day > 0 && day < 30) {
            let c = 0;
            switch (month - 1) {
                case 11: c += 30;
                case 10: c += 31;
                case 9: c += 30;
                case 8: c += 31;
                case 7: c += 30;
                case 6: c += 31;
                case 5: c += 30;
                case 4: c += 31;
                case 3: c += 30;
                case 2: {
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                        c += 29;
                    } else {
                        c += 28;
                    }
                }
                case 1: c += 31;
            }
            console.log("在" + year + "中,你輸入的是當前第" + (c + day) + "天");

        } else {
            console.log("天數錯誤");
        }
    } else {
        console.log("月份錯誤");
    }
} else {
    console.log("年份輸入錯誤");
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM