2013-09-29 32 views
-1

我是新來的VB和一個類項目,我們要做一個改變計算器,類似於在這裏問一個問題。我有欠款和金額支付標籤和文本框。如果所擁有的金額大於支付的金額,程序應該顯示消息以提醒客戶,並告訴他們還有多少要支付。顯示「支付的金額」是不夠的更改計算器,VB VS2012

我計算出來,但仍然由於所顯示的消息中的量是-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

更改代碼以這種方式

'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)是一個布爾表達式,你可以直接分配給一個布爾變量。然後你可以在顯示你的消息之前測試這個布爾值。

因此,不需要引入錯誤的這些轉換。