【发布时间】:2019-04-03 03:00:54
【问题描述】:
这是一个家庭作业,用于制作一个计算度假屋租金的应用。所有的计算都必须通过函数来完成。每当我运行程序时,所有输出都返回零(0.00 美元)。我可以说这是因为“0”被传递给函数,而不是从用户输入中检索到的值。由于验证功能能够很好地检索该输入,因此我不知道问题出在哪里。
我尝试将我的函数从 FUNCTION_NAME(PARAM1, PARAM2...) 重组为 VARIABLE = FUNCTION_NAME(PARAM1, PARAM2...),但没有奏效。
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
'Declare variables and constants
Dim strFirstName As String
Dim strLastName As String
Dim strPhoneNumber As String
Dim strEmailAddress As String
Dim intDaysToRent As Integer
Dim strSeason As String
Dim strBuyerState As String
Dim dblSubTotal As Double
Dim dblTaxAmount As Double
Dim dblFinalTotal As Double
Const dblOffSeason As Double = 50
Const dblPeakSeason As Double = 150
Const dblStandardSeason As Double = 100
Const dbl14DayDiscount As Double = 0.95
Const dbl30DayDiscount As Double = 0.9
Const dblTaxRate As Double = 0.1
'Validate inputs
If ValidateInputs(strFirstName, strLastName, strPhoneNumber, strEmailAddress, intDaysToRent, strSeason, strBuyerState) = True Then
'Calculate subtotal
dblSubTotal = CalculateSubTotal(intDaysToRent, strSeason, dbl14DayDiscount, dbl30DayDiscount, dblSubTotal)
'Calculate tax amount
dblTaxAmount = CalculateTaxAmount(dblSubTotal, strBuyerState, dblTaxRate, dblTaxAmount)
'Calculate final total
dblFinalTotal = CalculateFinalTotal(dblSubTotal, dblTaxAmount, dblFinalTotal)
____________________________________________________________
Function ValidateInputs(ByVal FirstName As String, ByVal LastName As String, ByVal PhoneNumber As String, ByVal EmailAddress As String, ByVal DaysToRent As Integer, ByVal Season As String, ByVal BuyerState As String)
'Main validation procedure
If ValidateFirstName(FirstName) = True Then
If ValidateLastName(LastName) = True Then
If ValidatePhoneNumber(PhoneNumber) = True Then
If ValidateEmailAddress(EmailAddress) = True Then
If ValidateDaysToRent(DaysToRent) = True Then
If ValidateSeason(Season) = True Then
If ValidateBuyerState(BuyerState) = True Then
Return True
Else Return False
End If
Else Return False
End If
Else Return False
End If
Else Return False
End If
Else Return False
End If
Else Return False
End If
Else
Return False
Exit Function
End If
End Function
____________________________________________________________
'First name validation subroutine
Function ValidateFirstName(ByRef FirstName As String) As Boolean
'Check that input exists
If txtFirstName.Text Is String.Empty Then
txtFirstName.BackColor = Color.Yellow
txtFirstName.Focus()
MessageBox.Show("Please enter the renter's first name.")
Return False
Exit Function
'Check that input is a valid string
ElseIf IsNumeric(txtFirstName.Text) Then
txtFirstName.BackColor = Color.Yellow
txtFirstName.Focus()
MessageBox.Show("Please enter letters only.")
Return False
Exit Function
Else
FirstName = txtFirstName.Text
Return True
End If
End Function
____________________________________________________________
'Last name validation subroutine
Function ValidateLastName(ByRef LastName As String) As Boolean
'Check that input exists
If txtLastName.Text Is String.Empty Then
txtLastName.BackColor = Color.Yellow
txtLastName.Focus()
MessageBox.Show("Please enter the renter's last name.")
Return False
Exit Function
'Check that input is valid string
ElseIf IsNumeric(txtLastName.Text) Then
txtLastName.BackColor = Color.Yellow
txtLastName.Focus()
MessageBox.Show("Please enter letters only.")
Return False
Exit Function
Else
LastName = txtLastName.Text
Return True
End If
End Function
____________________________________________________________
'Validate phone number subroutine
Function ValidatePhoneNumber(ByRef PhoneNumber As String) As Boolean
'Check that input exists
If txtPhoneNumber.Text Is String.Empty Then
txtPhoneNumber.BackColor = Color.Yellow
txtPhoneNumber.Focus()
MessageBox.Show("Please enter the renter's phone number.")
Return False
Exit Function
'Check that input is valid string
ElseIf IsNumeric(txtPhoneNumber.Text) Then
PhoneNumber = txtPhoneNumber.Text
Return True
Else
txtPhoneNumber.BackColor = Color.Yellow
txtPhoneNumber.Focus()
MessageBox.Show("Please enter numbers only.")
Return False
Exit Function
End If
End Function
____________________________________________________________
'Validate email address subroutine
Function ValidateEmailAddress(ByRef EmailAddress As String) As Boolean
'Check that input exists
If txtEmail.Text Is String.Empty Then
txtEmail.BackColor = Color.Yellow
txtEmail.Focus()
MessageBox.Show("Please enter the renter's email address.")
Return False
Exit Function
Else
EmailAddress = txtEmail.Text
Return True
End If
End Function
____________________________________________________________
'Validate days to rent subroutine
Function ValidateDaysToRent(ByVal DaysToRent As String) As Boolean
'Check that input exists
If txtDaysRented.Text Is String.Empty Then
txtDaysRented.BackColor = Color.Yellow
txtDaysRented.Focus()
MessageBox.Show("Please enter the number of days the condo will be rented.")
Return False
'Check that input is valid string
ElseIf IsNumeric(txtDaysRented.Text) Then
DaysToRent = txtDaysRented.Text
Return True
Else
txtDaysRented.BackColor = Color.Yellow
txtDaysRented.Focus()
MessageBox.Show("Please enter numbers only.")
Return False
Exit Function
End If
End Function
____________________________________________________________
'Season validation subroutine
Function ValidateSeason(ByVal Season As String) As Boolean
'Check that input exists
If cboSeason.Text Is String.Empty Then
cboSeason.BackColor = Color.Yellow
cboSeason.Focus()
MessageBox.Show("Please enter the season the condo will be rented in.")
Return False
Exit Function
Else
Season = cboSeason.Text
Return True
End If
End Function
____________________________________________________________
'State validation subroutine
Function ValidateBuyerState(ByVal BuyerState As String) As Boolean
'Check that input exists
If cboState.Text Is String.Empty Then
cboState.BackColor = Color.Yellow
cboState.Focus()
MessageBox.Show("Please enter the season the renter's state of residence.")
Return False
Exit Function
Else
BuyerState = cboState.Text
Return True
End If
End Function
____________________________________________________________
'Subtotal Calcultion Function
Function CalculateSubTotal(ByVal DaysToRent As Integer, ByRef Season As String, ByVal Discount14 As Double, ByVal Discount30 As Double, ByRef Subtotal As Double) As Double
'Declare variable
Dim dblSubTotal As Double
Dim dblSeasonRate As Double
Dim strSeason As String
cboSeason.Text = strSeason
'Determine season rate
If Season = "Off Season" Then
dblSeasonRate = 50
ElseIf Season = "Peak Season" Then
dblSeasonRate = 150
ElseIf Season = "Standard Season" Then
dblSeasonRate = 100
End If
'Calculate subtotal
dblSubTotal = DaysToRent * dblSeasonRate
'Calculate discount (if applicable)
If DaysToRent > 14 And DaysToRent < 31 Then
dblSubTotal = Subtotal * Discount14
ElseIf DaysToRent > 30 Then
dblSubTotal = Subtotal * Discount30
End If
Return dblSubTotal
End Function
____________________________________________________________
'Tax amount calculation function
Function CalculateTaxAmount(ByVal Subtotal As Double, ByVal State As String, ByVal TaxRate As Double, ByRef TaxAmount As Double) As Double
'Calculate tax rate
If State = "Florida" Then
TaxAmount = 0
Else
TaxAmount = Subtotal * TaxRate
End If
Return TaxAmount
End Function
____________________________________________________________
'Final total calculation function
Function CalculateFinalTotal(ByVal Subtotal As Double, ByVal TaxAmount As Double, ByRef FinalTotal As Double) As Double
'Calculate final total
FinalTotal = Subtotal + TaxAmount
Return FinalTotal
End Function
【问题讨论】:
-
向我们展示
ValidateInputs函数的代码。该方法是否正确分配了值? -
您实际上在哪里为
intDaysToRent赋值?该代码中没有任何地方,这是肯定的。除非ValidateInputs的每个参数都声明为ByRef并将输入分配给这些参数,否则您不会在任何地方这样做。当然,如果这就是ValidateInputs所做的,那么我想知道你为什么没有向我们展示,因为它是代码的关键部分。 -
我添加了验证功能的代码。
-
所有那些嵌套的
If语句都是不必要的,而且读起来很痛苦。只需使用一个If语句并使用AndAlso来组合布尔条件。此外,将Boolean表达式与Boolean文字进行比较是没有意义的。True = True是True和False = True是False,所以结果将等于原始表达式,所以只需使用原始表达式。最后,测试Boolean表达式然后显式返回相同的值是没有意义的。只需返回Boolean表达式。待定... -
所有这意味着您的
ValidateInputs方法简化为带有长Boolean表达式的单个Return语句:Return ValidateFirstName(FirstName) AndAlso ValidateLastName(LastName) AndAlso … AndAlso ValidateBuyerState(BuyerState)。只要任何单个方法调用返回False,整个表达式就会短路并且ValidateInputs方法返回False。如果每个单独的方法都返回True,validateInputs返回True。