VB程序不工作,应该很简单

时间:2014-11-18 03:15:48

标签: vb.net

这是一项家庭作业,据我所知,我有我应该做的一切。但由于某种原因它不起作用。如果我输入“50000”作为物业价值,“。6”作为评估百分比,“。7”作为评估费率,我应该得到“210美元”的物业税,但我一直只得到零,不知道是什么我错过了。它要到周日才会到期,所以如果你只想给我一个提示,也会受到赞赏。我已经阅读了几次说明和代码,但我仍然没有看到我的错误。我一直在看“propertytaxLabel.Text = CDec(decTax)”,就像我在那里弄乱了一些东西。

Public Class Form1
Private Sub clearButton_Click(sender As Object, e As EventArgs) Handles clearButton.Click
    valueTextBox.Clear()
    percentTextBox.Clear()
    rateTextBox.Clear()
    propertytaxLabel.Text = ""
End Sub

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
    Me.Close()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    Dim result As DialogResult
    result = MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.YesNo)
    If result = DialogResult.No Then
        e.Cancel = True
    End If
End Sub
Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click
    Try
        Dim decValue, decPercent, decRate, decTax As Decimal
        decValue = CDec(valueTextBox.Text)
        decPercent = CDec(percentTextBox.Text)
        decRate = CDec(rateTextBox.Text)

        propertytaxLabel.Text = CStr(decTax)

        If proceduresRadioButton.Checked = True Then
            TaxCalcProc(decValue, decPercent, decRate, decTax)
        Else
            decTax = TaxCalcFunc(decValue, decPercent, decRate)
        End If
        FormatCurrency(decTax)
    Catch ex As Exception
        MessageBox.Show("Error: Did you enter the data correctly?")
    End Try
End Sub
Private Sub TaxCalcProc(ByVal propVal As Decimal, ByVal perc As Decimal, ByVal rate As Decimal, ByRef tax As Decimal)
    Dim AssessedValue As Decimal
    Try
        AssessedValue = propVal * perc
        tax = AssessedValue / 100 * rate
        If rate > 1 Or perc > 1 Then
            Throw New Exception("Tax Greater Than Value. Did you use the decimal point?")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
Private Function TaxCalcFunc(ByVal propVal As Decimal, ByVal perc As Decimal, ByVal rate As Decimal) As Decimal
    Dim tax As Decimal
    Dim AssessedValue As Decimal
    Try
        AssessedValue = propVal * perc
        tax = AssessedValue / 100 * rate
        If rate > 1 Or perc > 1 Then
            Throw New Exception("Tax Greater Than Value. Did you use the decimal point?")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Return 0
    End Try
    Return Tax
End Function

结束班

3 个答案:

答案 0 :(得分:1)

您在宣布之后将decTax打印到标签上,但在计算之后却没有。

尝试在按钮点击事件中将propertytaxLabel.Text = CStr(decTax)移至FormatCurrency(decTax)之后。

答案 1 :(得分:0)

我的VB生锈了,但你没有任何机会试图将“50,000”转换为数字,而且它失败了,因为它有一个逗号?大多数字符串 - > int和string - >浮点转换(在其他语言中)不喜欢逗号。

答案 2 :(得分:0)

首先,您应该使用Visual Studio研究调试和设置断点。这将使解决这个问题非常容易。除此之外,您可以通过以下方式隔离和测试您的功能:

Private Sub calculateButton_Click(sender As Object, e As EventArgs) Handles calculateButton.Click
    Dim decTax As Decimal
    TaxCalcProc(50000D, 0.6D, 0.7D, decTax)
    Dim resultString = Format(decTax, "C")
    MessageBox.Show("decTax = " + resultString, "Test", MessageBoxButtons.Ok)
End Sub

另外,改变:

AssessedValue / 100 * rate

为:

AssessedValue / 100D * rate 

你可能对Int有一个无意的情况,因为除以100. 100D告诉编译器你想要一个十进制数。