2012-02-23 65 views
1

我正在處理一項作業,其中程序需要根據重量計算並顯示包裹運輸的總成本,以及是否將其運輸至美國大陸地區,阿拉斯加或夏威夷。但是,當單擊「計算」按鈕時,應顯示總計的標籤留空。我仔細研究過這一點,並嘗試將計算放在「If」語句末尾的不同部分。這是我迄今,任何幫助,將不勝感激:Visual Basic - 「If」聲明不顯示標籤中的結果

Dim decWeight As Decimal 
Dim decTotalCost As Decimal 
Dim decDestination As Decimal 
Dim decRate As Decimal 

If IsNumeric(txtWeight.Text) Then 

    decWeight = Convert.ToDecimal(txtWeight.Text) 


    If decWeight <= 30 > 0 Then 

     If radContinental.Checked Then 
      decDestination = 1 
     ElseIf radHawaii.Checked Then 
      decDestination = 1.2 
     ElseIf radAlaska.Checked Then 
      decDestination = 1.26 
     End If 

     If decWeight <= 2 Then 
      decRate = 3.69 
     ElseIf decWeight <= 4 > 2 Then 
      decRate = 4.86 
     ElseIf decWeight <= 6 > 4 Then 
      decRate = 5.63 
     ElseIf decWeight <= 8 > 6 Then 
      decRate = 5.98 
     ElseIf decWeight <= 10 > 8 Then 
      decRate = 6.28 
     ElseIf decWeight <= 30 > 10 Then 
      decRate = 15.72 
     End If 

     decTotalCost = decRate * decDestination 
     lblTotalCost.Text = decTotalCost.ToString("C") 

    ElseIf decWeight <= 0 Then 
     MsgBox("Please Enter a Positive Weight.", , "Input Error") 
    ElseIf decWeight > 30 Then 
     MsgBox("You Have Entered " & decWeight.ToString() & ". Please Enter a Weight Under 30 Pounds", , "Input Error") 
    End If 


ElseIf txtWeight.Text = "" Then 
    MsgBox("Please Enter the Weight", , "Input Error") 

Else : MsgBox("Please Enter a Number", , "Input Error") 
End If 
+0

'decWeight <= 30 > 0'應該是'decWeight <= 30'? – dashesy 2012-02-23 01:05:36

回答

2

你應該如果嘗試着這樣一句話:如果decWeight < = 30和decWeight> 0,則

這將檢查decWeight小於或等於30,並確保它是'非零'希望這有助於:-)

+0

這似乎已經修復了它,雖然我不明白爲什麼如果decWeight <= 30且decWeight> 0然後工作,但如果decWeight <= 30並且decWeight> 0則不。我認爲這是同一件事?編輯 - 啊,我想這不是,我確實必須包括And。謝謝! – user1227201 2012-02-23 01:25:55

+2

記住舊的數學規則,從左到右。那麼「decWeight <= 30」的結果是否大於零?在VB中,True = 1且False = 0,因此如果「decWeight <= 30」爲true,則「True> 0」爲True。 由於@ riptide464c說,你需要將你的公式分成2個語句,因此也就是AND。 現在它讀取:是「decWeight <= 30」的結果是真並且「decWeight> 0」的結果是真的? – Jim 2012-02-23 20:21:37

+1

所以所有其他If語句也需要修復。 – Jim 2012-02-23 20:23:34