编写一个程序,要求用户输入月份并显示该月份的天数(考虑闰年)

Write a program that asks the user for a month number and displays the number of days that month has (putting a leap year in mind)

我不太明白为什么我的代码不起作用,对于 1900 年(不是闰年),它仍然显示 29 天

    Dim Month, Year As Integer
    Dim X, Y As Single

    Console.WriteLine("Which month is it?")
    Month = Console.ReadLine()

    Console.WriteLine("Which year is it?")
    Year = Console.ReadLine()

    Select Case Month
        Case 4, 6, 9, 11
            Console.WriteLine("Number of days is 30.")
        Case 2
            If Year Mod 100 = 0 Then
                X = Year Mod 400
            Else
                Y = Year Mod 4
            End If
            If X = 0 Or Y = 0 Then
                Console.WriteLine("The number of days is 29.")
            Else
                Console.WriteLine("The number of Days is 28.")
            End If
        Case Else
            Console.WriteLine("Number of days is 31")
    End Select
    Console.ReadLine()

我会去掉 XY - 正如我在评论中所说,其中一个 将始终等于零,因为您的代码只会更改其中的 一个 。虽然你 可以 使你的 If 的两个分支关于 100 年规则设置 相同的 变量,然后检查它是否 0, 我只是将所有逻辑直接放在一个 If:

If Year Mod 400 = 0 Or (Year Mod 4 = 0 And Not Year Mod 100 = 0) Then
    Console.WriteLine("The number of days is 29.")
Else
    Console.WriteLine("The number of Days is 28.")
End If