我是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
答案 0 :(得分: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)
是一个布尔表达式,您可以直接赋值给布尔变量。然后你可以在显示你的消息之前测试这个布尔值。
因此,不需要这些转换会引入错误。