-1

我是 VB 的新手,对于一个课堂项目,我们将制作一个更改计算器,类似于之前在这里提出的问题。我有一个欠款和支付金额标签和文本框。如果拥有的金额大于支付的金额,程序应该显示消息提醒客户并告诉他们还需要支付多少。

我想通了,但消息中显示的到期金额为-1。

示例:欠款:25 已付金额:10 消息将显示,已付金额少于欠款。请多付$-1。

我不确定我做错了什么,我被卡住了。任何帮助将不胜感激!

Option Strict On
Option Explicit On

Public Class Form1
Dim AmountPaid As Double
Dim AmountOwed As Double


Private Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles       CalculateButton.Click
    'input amount owed from OwedMaskedTextBox
    'input paid amount from PaidTextBox
    AmountOwed = Convert.ToDouble(OwedTextBox.Text)
    AmountPaid = Convert.ToDouble(PaidTextBox.Text)


    'calculate difference of amount owed and paid
    'display an alert message if paid amount is less than what is owed
    Dim dif As Double
    Dim result As Double = 0

    result = CDbl(AmountPaid < AmountOwed)


    dif = AmountPaid - AmountOwed
    If CBool(result) Then
        AlertLabel.Text = "Amount paid is less than what is owed." &
            "Please pay $ " & result & "  more."

    Else
        AlertLabel.Text = ""
    End If

    'display the result
    'let totallabel change text to display the difference
    TotalLabel.Text = "Change: " &
        dif.ToString()

End Sub
End Class
4

1 回答 1

0

这样改代码

'calculate difference of amount owed and paid
'display an alert message if paid amount is less than what is owed
Dim result As Boolean

result = (AmountPaid < AmountOwed)
dif = AmountPaid - AmountOwed
If result Then
  .....

该表达式(AmountPaid < AmountOwed)是一个布尔表达式,您可以直接分配给一个布尔变量。然后你可以在显示你的消息之前测试这个布尔值。

所以不需要这些引入错误的转换。

于 2013-09-29T17:09:16.380 回答