【问题标题】:Visual Basic Prodcedures and Functions [closed]Visual Basic 过程和函数
【发布时间】:2017-11-07 03:11:59
【问题描述】:

我正在尝试使用程序和函数创建一个所得税计算器。代码应该是不言自明的。一切都返回零值,有人能给我一些关于我哪里出错的见解吗?

Partial Class TaxCalcualtor
Inherits System.Web.UI.Page
Private totalWages As Decimal
Private taxesWithheld As Decimal
Private deductions As Integer
Private married As Boolean
Private taxLiability As Decimal
Private federalTaxes As Decimal
Private stateTaxes As Decimal
Private localTaxes As Decimal

Function SingleFunction(ByVal totalWages As Decimal, ByVal married As Boolean, ByVal taxLiability As Decimal) As Decimal
    Dim taxRate As Decimal
    totalWages = CDec(TextBoxWages.Text)
    If married = False And totalWages < 25000.0 Then
        taxRate = 0.15
    ElseIf married = False And totalWages >= 25000.0 And totalWages < 45000.0 Then
        taxRate = 0.18
    ElseIf married = False And totalWages >= 45000.0 And totalWages < 65000.0 Then
        taxRate = 0.22
    ElseIf married = False And totalWages >= 65000.0 And totalWages < 85000.0 Then
        taxRate = 0.28
    ElseIf married = False And totalWages >= 85000.0 And totalWages < 100000.0 Then
        taxRate = 0.32
    ElseIf married = False And totalWages >= 100000.0 Then
        taxRate = 0.35
    End If
    taxLiability = taxRate * totalWages
    Return taxLiability
End Function

Function SingleMarriedFunction(ByVal totalWages As Decimal, ByVal married As Boolean, ByVal taxLiability As Decimal) As Decimal
    Dim taxRate As Decimal
    totalWages = CDec(TextBoxWages.Text)
    If married = True And totalWages < 25000.0 Then
        taxRate = 0.13
    ElseIf married = True And totalWages >= 25000.0 And totalWages < 45000.0 Then
        taxRate = 0.16
    ElseIf married = True And totalWages >= 45000.0 And totalWages < 65000.0 Then
        taxRate = 0.18
    ElseIf married = True And totalWages >= 65000.0 And totalWages < 85000.0 Then
        taxRate = 0.2
    ElseIf married = True And totalWages >= 85000.0 And totalWages < 100000.0 Then
        taxRate = 0.22
    ElseIf married = True And totalWages >= 100000.0 Then
        taxRate = 0.24
    End If
    taxLiability = taxRate * totalWages
    Return taxLiability
End Function

Function FederalTax(ByVal taxWithheld As Decimal, ByVal taxLiability As Decimal, ByVal deductions As Integer) As Decimal
    Dim federalTaxes As Decimal
    taxesWithheld = CDec(TextBoxTaxesWithheld.Text)
    federalTaxes = taxWithheld - taxLiability - (deductions * 750)
    Return federalTaxes
End Function

Function StateTax(ByVal totalWages As Decimal) As Decimal
    Dim stateTaxes As Decimal
    totalWages = CDec(TextBoxWages.Text)
    stateTaxes = totalWages * 0.06
    Return stateTaxes
End Function

Function LocalTax(ByVal totalWages As Decimal) As Decimal
    Dim localTaxes As Decimal
    totalWages = CDec(TextBoxWages.Text)
    localTaxes = totalWages * 0.01
    Return localTaxes
End Function

Sub DisplayData()
    Label7.Text += "Tax Liability: " + CStr(taxLiability)
    Label7.Text += "Federal Taxes: " + CStr(federalTaxes)
    Label7.Text += "State Taxes: " + CStr(stateTaxes)
    Label7.Text += "Local Taxes: " + CStr(localTaxes)
End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    DisplayData()
End Sub
End Class

【问题讨论】:

  • 期望行为:计算税收责任、联邦、州和地方税问题:所有值都返回 0
  • 您正在显示从未被初始化/修改过的值。如果没有更多代码,则永远不会修改 taxLiability。计算函数永远不会被调用。
  • 您永远不会调用“SingleFunction”和/或“SingleMarriedFunction”。也不应该被称为 MarriedFunction 不是因为它改变了任何东西。此外,您不应将全局变量与您在函数中传递的局部变量的名称相同。函数的目标是从您传递给它的任何信息中返回一个值。您正在许多地方更改纳税义务,但也在许多地方进行了申报。你因此而感到困惑。计算机不知道您真正想要的女巫并返回给您本地的。

标签: vb.net function procedures


【解决方案1】:

给你。看看我做了什么,这样你就可以理解我和你的有什么不同。我还假设您有一个名为 TextBoxDeductions 的文本框。您还应该有一个 CheckBox 以单击是否已婚。这个 CheckBox 我叫 CheckBoxMarried。我结合了你的 2 个函数 SingleFunction 和 SingleMarriedFunction。希望它对你有意义!

   Function GetTaxLiability(ByVal totalWages As Decimal, ByVal married As Boolean) As Decimal
        Dim taxRate As Decimal
        If married = True And totalWages < 25000.0 Then
            taxRate = 0.13
        ElseIf married = True And totalWages >= 25000.0 And totalWages < 45000.0 Then
            taxRate = 0.16
        ElseIf married = True And totalWages >= 45000.0 And totalWages < 65000.0 Then
            taxRate = 0.18
        ElseIf married = True And totalWages >= 65000.0 And totalWages < 85000.0 Then
            taxRate = 0.2
        ElseIf married = True And totalWages >= 85000.0 And totalWages < 100000.0 Then
            taxRate = 0.22
        ElseIf married = True And totalWages >= 100000.0 Then
            taxRate = 0.24
        End If
        If married = False And totalWages < 25000.0 Then
            taxRate = 0.15
        ElseIf married = False And totalWages >= 25000.0 And totalWages < 45000.0 Then
            taxRate = 0.18
        ElseIf married = False And totalWages >= 45000.0 And totalWages < 65000.0 Then
            taxRate = 0.22
        ElseIf married = False And totalWages >= 65000.0 And totalWages < 85000.0 Then
            taxRate = 0.28
        ElseIf married = False And totalWages >= 85000.0 And totalWages < 100000.0 Then
            taxRate = 0.32
        ElseIf married = False And totalWages >= 100000.0 Then
            taxRate = 0.35
        End If
        Return taxRate * totalWages
    End Function

    Function FederalTax(ByVal taxWithheld As Decimal, ByVal taxLiability As Decimal, ByVal deductions As Integer) As Decimal
        Return taxWithheld - taxLiability - (deductions * 750)
    End Function

    Function StateTax(ByVal totalWages As Decimal) As Decimal
        Return totalWages * 0.06
    End Function

    Function LocalTax(ByVal totalWages As Decimal) As Decimal
        Return totalWages * 0.01
    End Function

    Sub DisplayData()
        Dim TaxLiability As Decimal
        If CheckBoxMarried.Checked = True Then
            TaxLiability = GetTaxLiability(CDec(TextBoxWages.Text), True)
        Else
            TaxLiability = GetTaxLiability(CDec(TextBoxWages.Text), False)
        End If

        Label7.Text += "Tax Liability: " + CStr(TaxLiability)
        Label7.Text += "Federal Taxes: " + CStr(FederalTax(CDec(TextBoxTaxesWithheld.Text), TaxLiability, CDec(TextBoxDeductions.text)))
        Label7.Text += "State Taxes: " + CStr(StateTax(CDec(TextBoxWages.Text)))
        Label7.Text += "Local Taxes: " + CStr(LocalTax(CDec(TextBoxWages.Text)))
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DisplayData()
    End Sub

【讨论】:

    猜你喜欢
    • 2016-07-07
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    相关资源
    最近更新 更多