我正在开始VB课程并刚刚制作了我的第一个程序,一个工资计算器,计算加班和双倍工资的额外奖金,要求用户只输入工作小时数和工资率。到目前为止它运行正如我设计的一个缺陷。如果用户在不到8小时内进入,我得到的OT工作小时数为负数,这完全扰乱了工资的计算。我认为在小于或等于零的小时内输入条件语句会解决它,但它仍会打印并计算负数。
有人可以如此友善地指出我在这里缺少的东西吗?我确信它是一件我想不起的简单事。
提前谢谢!
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim dblHoursWorked As Double = 0 'to hold hours worked
Dim dblHourlyPayRate As Double = 0 'to hold hourly pay rate
Dim dblStandardHours As Double = 0 'hold standard hours worked in a day which is always equal to 8
Dim dblOtHours As Double = 0 ' to hold ot hours worked which is dblHoursWorked minus 8 then minus 4 if amount is greater than 4
Dim dblOtCache As Double = 0 ' to hold variable for OT hours over 4
Dim dblDtHours As Double = 0 'to hold dt hours worked which is ot hours minus 4 if ot is greater than 4
Dim dblStandardPay As Double = 0 'to hold standard pay rate which is always <=8
Dim dblOtPayRate As Double = 0 'to hold ot pay rate which is dblHourlyPayRate/2 + dblHourlyPayRate
Dim dblDtPayRate As Double = 0 'to hold dt pay rate which is dblHourlyPayRate*2
Dim dblOtPay As Double = 0 'Calcuate OT pay
Dim dblDtPay As Double = 0 'Calculate DT pay
Dim dblGrossPay As Double = 0 'to hold gross pay
'making labels visible
Label2.Visible = True
Label6.Visible = True
lblPrintOtRate.Visible = True
lblPrintDtRate.Visible = True
'Read the values from text box controls
dblHoursWorked = CDbl(txtHoursWorked.Text)
'Read value from text box controls
dblHourlyPayRate = CDbl(txtPayRate.Text)
'Calculate Standard Hours
If dblHoursWorked >= 8 Then dblStandardHours = 8
If dblHoursWorked < 8 Then dblStandardHours = dblHoursWorked
'Display Standard Hours
lblStandardHours.Text = CStr(dblStandardHours)
'Calculate OT pay rate
dblOtPayRate = dblHourlyPayRate / 2 + dblHourlyPayRate
'calculate dt pay rate
dblDtPayRate = dblHourlyPayRate * 2
'Calculate OT hours cache
dblOtCache = dblHoursWorked - 8
'Conditional statement for OT hours if OT Cache calculations are below 0
If dblOtCache <= 0 Then dblOtHours = Nothing
'calculate ot hours actual if ot is equal to or less than 4
If dblOtCache <= 4 Then dblOtHours = dblOtCache
'conditional statements of what to print for ot hours if ot hours are greater than 4
If dblOtCache > 4 Then dblOtHours = 4
If dblOtCache > 4 Then dblDtHours = dblOtCache - 4
'Display OT Hours worked
lblOtHours.Text = CStr(dblOtHours)
'Calculate DT Hours
lblDtHours.Text = CStr(dblDtHours)
'Calculate Standard Pay
dblStandardPay = dblStandardHours * dblHourlyPayRate
'Calculate OT Pay
dblOtPay = dblOtPayRate * dblOtHours
'Calculate DT Pay
dblDtPay = dblDtPayRate * dblDtHours
'Calculate Gross Pay
dblGrossPay = dblStandardPay + dblOtPay + dblDtPay
'Display Gross Pay
txtTotalDailyPay.Text = "$ " + FormatNumber(CStr(dblGrossPay), 2)
'Display OT Rate
lblPrintOtRate.Text = "$ " + FormatNumber("$ " + CStr(dblOtPayRate), 2) + " per hour"
'Display OT Rate
lblPrintDtRate.Text = "$ " + FormatNumber("$ " + CStr(dblDtPayRate), 2) + " per hour"
End Sub