我目前正在大学读计算机科学,并为编程设置了一些任务。我是VB的新手,所以我到目前为止编写的代码是非常基本的。
他们现在需要做的项目是输出用户输入相当数字的日期和年份。例如:
用户输入: 2016年1月12日
显示为: 2016年12月1日。
我已经完成了大部分工作,但现在我被困住了。我需要考虑闰年的考虑因素,这样如果有人在2016年2月29日输入,那么它将被允许,而如果它是29 2 2015年则不会。我现在的代码有一个复选框,允许用户勾选是否为闰年,如果不是则取消勾选。是否有一种简单的方法来输入闰年而不是全部放入? (如2000年,2004年,2008年,2012年......)。
感谢。
答案 0 :(得分:1)
我想我已经解决了问题,但感谢您的帮助,这是我的代码到目前为止(对不起,相当基本)。
Public Class formDayMonthYear
Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs) Handles btnCalculate.Click
'Calculates the Day, Month and Year in text according to the users input of numbers
'Variables
Dim iDay As Integer = txtDay.Text
Dim iMonth As Integer = txtMonth.Text
Dim iYear As Integer = txtYear.Text
Dim sDay As String
Dim sMonth As String
Dim sYear As String
'缺少复选框
If iMonth = 2 Then
If iDay > 28 Then
MsgBox(iDay & " is an incorrect day!", MsgBoxStyle.OkOnly, "Error!")
End If
End If
'Otherwise if it is ticked
Else
If iMonth = 2 Then
If iDay > 29 Then
MsgBox(iDay & " is a incorrect day!", MsgBoxStyle.OkOnly, "Error!")
End If
End If
End If
If iMonth = 4 Or iMonth = 6 Or iMonth = 9 Or iMonth = 11 Then
If iDay <= 0 Or iDay >= 31 Then
MsgBox(iDay & " is an incorrect day!", MsgBoxStyle.OkOnly, "Error!")
End If
ElseIf iMonth = 1 Or iMonth = 2 Or iMonth = 3 Or iMonth = 5 Or iMonth = 7 Or iMonth = 8 Or iMonth = 10 Or iMonth = 12 Then
If iDay <= 0 Or iDay >= 32 Then
MsgBox(iDay & " is an incorrect day!", MsgBoxStyle.OkOnly, "Error!")
End If
If iDay = 1 Or iDay = 21 Or iDay = 31 Then
sDay = iDay & "st"
ElseIf iDay = 2 Or iDay = 22 Then
sDay = iDay & "nd"
ElseIf iDay = 3 Or iDay = 23 Then
sDay = iDay & "rd"
Else : sDay = iDay & "th"
End If
If iMonth = 1 Then
sMonth = "January"
ElseIf iMonth = 2 Then
sMonth = "February"
ElseIf iMonth = 3 Then
sMonth = "March"
ElseIf iMonth = 4 Then
sMonth = "April"
ElseIf iMonth = 5 Then
sMonth = "May"
ElseIf iMonth = 6 Then
sMonth = "June"
ElseIf iMonth = 7 Then
sMonth = "July"
ElseIf iMonth = 8 Then
sMonth = "August"
ElseIf iMonth = 9 Then
sMonth = "September"
ElseIf iMonth = 10 Then
sMonth = "October"
ElseIf iMonth = 11 Then
sMonth = "November"
ElseIf iMonth = 12 Then
sMonth = "December"
Else
MsgBox(iMonth & " doesn't exist as a month!", MsgBoxStyle.OkOnly, "Error")
End If
sYear = iYear
txtDecision.Text = sDay & " of " & sMonth & " " & sYear
End If
End Sub
结束班
但正如有人在删除它之前建议的那样,我可以使用MOD来解决它,就像这样:
如果iYear mod 4 = 0则 “代码说是闰年 其他 '代码说不是闰年 结束如果
所以我的新代码段是:
'如果剩余的年/ 4 = 0那么
如果iYear Mod 4 = 0那么
'If the month is 2 (February) then
If iMonth = 2 Then
If iDay > 29 Then
MsgBox(iDay & " is an incorrect day!", MsgBoxStyle.OkOnly, "Error!")
End If
End If
'Otherwise if it is ticked
Else
If iMonth = 2 Then
If iDay > 28 Then
MsgBox(iDay & " is a incorrect day!", MsgBoxStyle.OkOnly, "Error!")
End If
End If
End If