对于包A,我的输出总是14.95。其他任何东西似乎都工作正常。
对于我需要的程序:
互联网服务提供商向其客户提供3个订阅套餐,并为非营利组织提供折扣
btnCalc_Click
Public Class Form1
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
'Declare variables and constant
Dim decTotal As Decimal
Dim intHours As Integer
Const decNonProfit As Decimal = 0.8D
Try
intHours = CInt(txtHoursUsed.Text)
If CInt(txtHoursUsed.Text) > 744 Then
MessageBox.Show("Monthly Hours Can't Exceed 744")
txtHoursUsed.Text = String.Empty
lblTotalDue.Text = String.Empty
End If
'Calculate A Package Total without discount
If radPackageA.Checked = True And intHours > 10 Then
decTotal = (9.95 + ((intHours - 10) * 2))
ElseIf intHours <= 10 Then
decTotal = 9.95
End If
'Calculate B Package Total without discount
If radPackageB.Checked = True And intHours > 20 Then
decTotal = (14.95 + ((intHours - 20) * 1))
ElseIf intHours <= 20 Then
decTotal = 14.95
End If
'Calculate C Package Total without discount
If radPackageC.Checked = True Then
decTotal = 19.95
End If
'Calculate Total for packages if Nonprofit is checked
If chkNonProfit.Checked = True Then
decTotal = decTotal * decNonProfit
End If
'Show total due
lblTotalDue.Text = decTotal.ToString("c")
Catch ex As Exception
MessageBox.Show("Input Error")
End Try
End Sub
btnClear_Click
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
radPackageA.Checked = False
radPackageB.Checked = False
radPackageC.Checked = False
chkNonProfit.Checked = False
lblTotalDue.Text = String.Empty
txtHoursUsed.Text = String.Empty
End Sub
btnExit_Click
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
答案 0 :(得分:2)
您需要在选择检查中附上整个条件。发生的事情是,当repackageB.Checked为false时,始终会运行您的错误条件。
'Calculate A Package Total without discount
If radPackageA.Checked Then
If intHours > 10 Then
decTotal = (9.95 + ((intHours - 10) * 2))
ElseIf intHours <= 10 Then
decTotal = 9.95
End If
End If
'Calculate B Package Total without discount
If radPackageB.Checked Then
If intHours > 20 Then
decTotal = (14.95 + ((intHours - 20) * 1))
ElseIf intHours <= 20 Then
decTotal = 14.95
End If
End If
'Calculate C Package Total without discount
If radPackageC.Checked Then
decTotal = 19.95
End If
答案 1 :(得分:1)
您正在使用ElseIf
以错误的方式分解条件。
按照您的代码,您每次dectTotal = 14.95
和radPackageB.Checked = False
intHours <= 20
应该是这样的:
If radPackageB.Checked Then
If intHours > 20 Then
decTotal = (14.95 + ((intHours - 20) * 1))
ElseIf intHours <= 20 Then
decTotal = 14.95
End If
End If
所以请随意接受Mark的回答