vb.net Visual Basic 工资税计算器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38224178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Basic Salary tax calculator
提问by MynameisJeff
The process that I'm trying to create is when a user inputs a number it gets detected by the IF statement to display which tax rate it goes under 20%, 40% or 45%, the amount of tax and the salary after for example If 10,000 inputted the tax rate shows 20% of that which calculates the amount of tax "2000" and the salary after tax "8000".These are the calculations I'm trying to workout, "0 to 31,785 tax rate 20%", but "if less than 10,600 tax rate 0%", 31,786 to 150,000 tax rate 40% if less than 42,385 tax rate 0%", over15,000 tax rate 45%.
我试图创建的过程是当用户输入一个数字时,它会被 IF 语句检测到,以显示它低于 20%、40% 或 45% 的税率,例如税额和工资如果输入 10,000,税率显示为计算税额“2000”和税后工资“8000”的 20%。这些是我正在尝试的计算,“0 到 31,785 税率 20%”,但“如果低于10600税率0%”,31786到150000税率40%如果低于42385税率0%,超过15000税率45%。
I have managed to do that for one some IF statement but when I try to do that for the next IF statement the numbers get mixed up and display the wrong results or don't display the tax rate that suppose to show.
我已经设法为某个 IF 语句执行此操作,但是当我尝试为下一个 IF 语句执行此操作时,数字会混淆并显示错误的结果,或者不显示应该显示的税率。
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
回答by в?a???? в?н???
Your Ifand ElseIfstatements are failing to execute as you are expecting them as the conditional order and operators used do not make sense.
您的IfandElseIf语句未能按照您的预期执行,因为使用的条件顺序和运算符没有意义。
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
Once one of the Ifstatements has been evaluated, execution will jump into the block and will exit at the End If; the other If statements will not be evaluated. For any number of decSalary over 10600, decTax will always the result of itself be multiplied by 0.2. Additionally the third conditional will never be hit unless decSalary is between 10501-10599, and even then the result will still be the same as if the value was over 10600.
一旦其中一个If语句被评估,执行将跳转到块中并在End If;处退出。不会评估其他 If 语句。对于超过 10600 的任意数量的 decSalary,decTax 将始终将其结果乘以 0.2。此外,除非 decSalary 在 10501-10599 之间,否则永远不会命中第三个条件,即使如此,结果仍然与值超过 10600 相同。
You are setting the values of the labels before calculating the tax amount. You are declaring variables for all of the different tax amounts, however you are only using one of them in your code.
您在计算税额之前设置标签的值。您正在为所有不同的税额声明变量,但是您在代码中仅使用其中之一。
One last thing, it appears as though you are using TextBoxfor all of your input controls. There are specific controls for handling numeric input and I would suggest implementing them.
最后一件事,似乎您正在使用TextBox所有输入控件。有处理数字输入的特定控件,我建议实施它们。
Here's is an incomplete example of how to perform the logic and output that you are asking for defined as you have it written in your question:
这是一个不完整的示例,说明如何执行您要求的逻辑和输出,正如您在问题中所写的那样:
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

