2015-06-05 56 views
-1

您好,我在vb.net中遇到大於小於函數的問題。它的正確數字完美地工作,但不會與負數工作。這是我的代碼,我希望這可以幫助。大於,小於負數

Dim A, B, input As Integer 
A = (-38) 
B = (-173) 
txtD.Text = CStr(CInt(txtD.Text)) 
input = CInt(txtD.Text) 

If input <= 32 < CInt(A) Then 
    txtR.Text = "Water will freeze and Oxygen will boil at this Temperature" 
ElseIf input <= CInt(A) > CInt(B) Then 
    txtR.Text = "Water and Mercury will Freeze and Oxygen will boil at this temperature" 
End If 
+2

你還沒有給你的輸入,預期與實際結果的一個例子。我個人建議一次只表達一個條件並使用'AndAlso'(或其他)來組合它們 - 對我來說(作爲一個簡單的C#開發者)我不清楚CInt(B)'是如何輸入的評估。 –

+0

(也不清楚爲什麼你使用'CInt(A)'和'CInt(B)'時它們已經是整數。) –

+1

請注意,如果你有'Option Strict',你會收到錯誤消息已經由於代碼問題。我強烈建議你開始使用'Option Strict' ... –

回答

0

不幸的是,你的代碼在語法上是錯誤的:

If input <= 32 < CInt(A) Then 
    txtR.Text = "Water will freeze and Oxygen will boil at this Temperature" 
ElseIf input <= CInt(A) > CInt(B) Then 
    txtR.Text = "Water and Mercury will Freeze and Oxygen will boil at this temperature" 
End If 

需要包括其他的語句,如AndOr取決於你試圖達到的目標。

例如:

ElseIf input <= CInt(A) And input > CInt(B) Then 

在什麼變量AB設置爲這些不會計算爲true不過爲主。

作爲附加的註釋:

txtD.Text = CStr(CInt(txtD.Text)) 

是一個毫無意義的聲明。

txtD.Text返回一個字符串,因此不需要將其轉換爲整數,然後將字符串轉換爲自身。只需刪除它。

此外,AB已經是整數,您不必通過使用CInt將它們轉換爲整數。直接使用變量AB即可。

0

您需要And以加入您的條件。

喜歡的東西

 If input <= 32 And 32 < CInt(A) Then 
      txtR.Text = "Water will freeze and Oxygen ..." 
    ElseIf input <= CInt(A) And CInt(A) > CInt(B) Then 
      txtR.Text = "Water and Mercury will Freeze and Oxygen will ..." 
    End If 
+0

我覺得這將是一個很好的時間來介紹一個新手使用'AndAlso'而不是'And'來避免VB.Net常見的陷阱。你怎麼看? – Mystra007

相關問題