Visual studio 编写一个程序,要求用户输入月数并显示该月的天数(记住闰年)
我不太明白为什么我的代码不起作用,对于1900年(不是闰年),它仍然显示29天Visual studio 编写一个程序,要求用户输入月数并显示该月的天数(记住闰年),visual-studio,visual-studio-2010,Visual Studio,Visual Studio 2010,我不太明白为什么我的代码不起作用,对于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() S
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()
我要去掉
X
和Y
——正如我在评论中所说的,它们中的一个总是等于零,因为您的代码只会更改其中一个。虽然您可以将If
的两个分支设置为相同的变量,然后检查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
你确定它对非闰年有效吗?因为您似乎只会设置
X
或Y
,因此其中一个应该始终为0
。它总是给出“29”作为答案。即使X=0或Y=0