C# 判斷是否為閏年的條件各是


//try 沒增加異常數據處理
            Console.WriteLine("根據輸入的信息計算當年某個月份的天數,以及當年是否是閏年或平年,\n並判斷2月份特殊月份的天數。");
            Console.WriteLine("請輸入需要計算的年份:");
            int year = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("請輸入需要獲取的月份");
            int month = Convert.ToInt32(Console.ReadLine());

          
            switch (month)
            { 
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    Console.WriteLine("是31天");
                    break;
                case 2:
                    if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
                        //閏年的判斷條件格式
                        // 如果 年能被400整除 或者 年能被4整除 並且年不能被100整除。

                    {
                        Console.WriteLine("是閏年,2月當月的天數有29天");
                    }
                    else
                    {
                        Console.WriteLine("是平年,2月當月的天數有28天");
                    }
                    break;
                default:
                    Console.WriteLine("為30天");
                    break;
            }


免責聲明!

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



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