我正在尝试创建的流程是当用户输入一个由IF语句检测到的数字,以显示它低于20%,40%或45%的税率,税额和工资例如,如果输入10,000,则税率显示税额“2000”和税后“8000”的工资的20%。这些是我正在努力锻炼的计算,“0到31,785税率20 %“,但”如果低于10,600税率0%“,31,786至150,000税率40%,如果低于42,385税率0%”,超过15,000税率45%。
我已经设法为一个IF语句做了这个,但当我尝试为下一个IF语句执行此操作时,数字混淆并显示错误的结果或不显示假设显示的税率。< / p>
Public Class Form1
Private Sub button1_Click(sender As Object, e As EventArgs) Handles cmdCalc.Click
Dim decSalary, decTax, decTax2, decTax3, decAmount As Decimal
decSalary = textSalary.Text
decTax = decSalary * 0.2
decTax2 = decSalary * 0.4
decTax3 = decSalary * 0.45
decAmount = decSalary - decTax
lblTax.Text = FormatPercent(0.0, 0.2, 0.4, 0.45, TriState.UseDefault)
Return
lblAmount.Text = Format(decTax, "currency")
lblFinal.Text = Format(decAmount, "currency")
If (decSalary <= 10500) Then
decTax *= 0
ElseIf (decSalary >= 10600) Then
decTax *= 0.2
ElseIf (decSalary <= 31785) Then
decTax *= 0.2
End If
End Sub
Private Sub textBox1_TextChanged(sender As Object, e As EventArgs) Handles textSalary.TextChanged
'Dim decSalary, decTax, decTax2, decAmount As Decimal
' lblTax.Text = FormatPercent(0.0)
' lblAmount.Text = Format(decTax, "currency")
' lblFinal.Text = Format(decAmount, "currency")
' If decSalary <= 10500 Then
'decTax = 0
' ElseIf decSalary >= 10600 Then
' decTax = 0.2
' ElseIf decSalary <= 31785 Then
' decTax2 = decTax
' End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
Me.Close()
End Sub
Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
textSalary.Clear()
lblTax.Text = "0%"
lblAmount.Text = "0"
lblFinal.Text = "0"
End Sub
End Class
答案 0 :(得分:1)
您的If
和ElseIf
语句无法执行,因为您期望它们作为条件顺序而且使用的运算符没有意义。
If (decSalary <= 10500) Then ' For all integers less than/equal to 10500
decTax *= 0
ElseIf (decSalary >= 10600) Then ' For all integers more than/equal to 10600
decTax *= 0.2
ElseIf (decSalary <= 31785) Then ' For all integers less than/equal to 31785
decTax *= 0.2
End If
一旦评估了If
个语句之一,执行就会跳转到该区块并退出End If
;其他If语句将不会被评估。对于10600以上的任何数量的decSalary,decTax总是将其自身的结果乘以0.2。此外,除非decSalary介于10501-10599之间,否则第三个条件将永远不会被命中,即使这样,结果仍将与值超过10600的结果相同。
您在计算税额之前设置标签的值。您正在声明所有不同税额的变量,但您只在代码中使用其中一个。
最后一点,看起来好像是在为所有输入控件使用TextBox
。有处理数字输入的特定控件,我建议实现它们。
这里是一个不完整的示例,说明如何在您的问题中写下您要求定义的逻辑和输出:
Private Sub CalculateDisplayAmounts(ByVal salary As Decimal)
Dim taxRate As Decimal = GetTaxRate(salary)
Dim taxAmount As Decimal = salary * taxRate
Dim takeHomePay As Decimal = salary - taxAmount
taxRateLabel.Invoke(Sub(x) taxRateLabel.Text = FormatPercent(x), taxRate)
taxAmountLabel.Invoke(Sub(x) taxAmountLabel.Text = FormatCurrency(x), taxAmount)
takeHomeLabel.Invoke(Sub(x) takeHomeLabel.Text = FormatCurrency(x), takeHomePay)
End Sub
Private Function GetTaxRate(ByVal salaryAmount As Decimal) As Decimal
Dim taxRate As Decimal = 0D
If salaryAmount <= 31785D Then
If salaryAmount >= 10600D Then
taxRate = 0.2D
End If
ElseIf salaryAmount <= 150000 Then
If salaryAmount >= 42385D Then
taxRate = 0.4D
End If
Else
taxRate = 0.45D
End If
Return taxRate
End Function