嘿,我需要在vb.net中创建一个程序,询问用户的生日。该计划应该给出这个人出生的星期几和他们的占星星座。有谁能够帮我??请!
答案 0 :(得分:2)
Private Function Zodiac(ByVal Birthday As Date) As String
Dim year As Integer = Birthday.Year
Dim zodiacs = {
New With {.From = New Date(year, 1, 1), .[To] = New Date(year, 1, 19), .Zodiac = "Capricorn"},
New With {.From = New Date(year, 1, 20), .[To] = New Date(year, 2, 18), .Zodiac = "Aquarius"},
New With {.From = New Date(year, 2, 19), .[To] = New Date(year, 3, 20), .Zodiac = "Pisces"},
New With {.From = New Date(year, 3, 21), .[To] = New Date(year, 4, 19), .Zodiac = "Aries"},
New With {.From = New Date(year, 4, 20), .[To] = New Date(year, 5, 20), .Zodiac = "Taurus"},
New With {.From = New Date(year, 5, 21), .[To] = New Date(year, 6, 20), .Zodiac = "Gemini"},
New With {.From = New Date(year, 6, 21), .[To] = New Date(year, 7, 22), .Zodiac = "Cancer"},
New With {.From = New Date(year, 7, 23), .[To] = New Date(year, 8, 22), .Zodiac = "Leo"},
New With {.From = New Date(year, 8, 23), .[To] = New Date(year, 9, 22), .Zodiac = "Virgo"},
New With {.From = New Date(year, 9, 23), .[To] = New Date(year, 10, 22), .Zodiac = "Libra"},
New With {.From = New Date(year, 10, 23), .[To] = New Date(year, 11, 21), .Zodiac = "Scorpio"},
New With {.From = New Date(year, 11, 22), .[To] = New Date(year, 12, 21), .Zodiac = "Sagittarius"},
New With {.From = New Date(year, 12, 22), .[To] = New Date(year, 12, 31), .Zodiac = "Capricorn"}}
Return (From z In zodiacs Where (z.From <= Birthday And Birthday <= z.[To])).Single.Zodiac
End Function
第一个蹩脚的尝试:
Private Function Zodiac(ByVal Birthday As Date) As String
Dim year As Integer = Birthday.Year
Dim sodiacCapricorn() As DateTime = {New DateTime(year, 12, 22), New DateTime(year, 1, 19)}
Dim sodiacAquarius() As DateTime = {New DateTime(year, 1, 20), New DateTime(year, 2, 18)}
Dim sodiacPisces() As DateTime = {New DateTime(year, 2, 19), New DateTime(year, 3, 20)}
Dim sodiacAries() As DateTime = {New DateTime(year, 3, 21), New DateTime(year, 4, 19)}
Dim sodiacTaurus() As DateTime = {New DateTime(year, 4, 20), New DateTime(year, 5, 20)}
Dim sodiacGemini() As DateTime = {New DateTime(year, 5, 21), New DateTime(year, 6, 20)}
Dim sodiacCancer() As DateTime = {New DateTime(year, 6, 21), New DateTime(year, 7, 22)}
Dim sodiacLeo() As DateTime = {New DateTime(year, 7, 23), New DateTime(year, 8, 22)}
Dim sodiacVirgo() As DateTime = {New DateTime(year, 8, 23), New DateTime(year, 9, 22)}
Dim sodiacLibra() As DateTime = {New DateTime(year, 9, 23), New DateTime(year, 10, 22)}
Dim sodiacScorpio() As DateTime = {New DateTime(year, 10, 23), New DateTime(year, 11, 21)}
Dim sodiacSagittarius() As DateTime = {New DateTime(year, 11, 22), New DateTime(year, 12, 21)}
Select Case Birthday.DayOfYear
Case 1 To sodiacCapricorn(1).DayOfYear, sodiacCapricorn(0).DayOfYear To 366
Return "Capricorn"
Case sodiacAquarius(0).DayOfYear To sodiacAquarius(1).DayOfYear
Return "Aquarius"
Case sodiacPisces(0).DayOfYear To sodiacPisces(1).DayOfYear
Return "Pisces"
Case sodiacAries(0).DayOfYear To sodiacAries(1).DayOfYear
Return "Aries"
Case sodiacTaurus(0).DayOfYear To sodiacTaurus(1).DayOfYear
Return "Taurus"
Case sodiacGemini(0).DayOfYear To sodiacGemini(1).DayOfYear
Return "Gemini"
Case sodiacCancer(0).DayOfYear To sodiacCancer(1).DayOfYear
Return "Cancer"
Case sodiacLeo(0).DayOfYear To sodiacLeo(1).DayOfYear
Return "Leo"
Case sodiacVirgo(0).DayOfYear To sodiacVirgo(1).DayOfYear
Return "Virgo"
Case sodiacLibra(0).DayOfYear To sodiacLibra(1).DayOfYear
Return "Libra"
Case sodiacScorpio(0).DayOfYear To sodiacScorpio(1).DayOfYear
Return "Scorpio"
Case sodiacSagittarius(0).DayOfYear To sodiacSagittarius(1).DayOfYear
Return "Sagittarius"
Case Else
Return "?"
End Select
End Function
使用匿名类型和单个简洁的LINQ语句更容易理解恕我直言。