2013-10-09 25 views
0

我爲我的一個類做了這個循環,但由於某種原因循環不停止。 這裏是我的代碼有我不能得到這個循環停止

Dim strScore As String 
    Dim intNumTests As Integer 'counter 
    Dim sngSumTests As Single 'accumulator 
    Dim sngAverage As Single 

    strScore = InputBox("Enter the score. Click cancel when finished.", "Score Entry") 

    Do While strScore <> " " 
     intNumTests = intNumTests + 1  ' update Counter 
     sngSumTests = sngSumTests + Val(strScore) 
     strScore = InputBox("Enter the score. Click cancel when finished.", "Score Entry") 
    Loop 
+3

Visual Studio是一個IDE,而不是語言。作爲IDE,它沒有循環。請[編輯]標籤以指定您的代碼使用的實際語言(以便它對未來的讀者有用,並使其獲得觀看該標籤的人的注意力)。另外,您可以嘗試使用調試器來幫助找出造成問題的原因。在代碼的第一行設置一個斷點並逐步遍歷它一次或兩次。 –

回答

2

對於循環停止,您必須InputBox中鍵入一個空格,而不是實際的單詞space但按空格鍵一次。

而不是

Do While strScore <> " " 

你真正想要的是什麼

Do While strScore <> "" AndAlso strScore <> " " 

Do While strScore.Length <> 0 AndAlso strScore <> " " 

現在Cancel按鈕將正常工作,並能持續,如果你不打算t鍵入任何內容並按OK按鈕。

如果您在InputBox中按下OK按鈕並且輸入爲空,它將返回" "(空格)。
如果按的InputBox的Cancel按鈕,它會返回""(空字符串)

編輯: 原來按OK與.NET Framework 4中也返回(空字符串)一個空的InputBox所以這是不可能的?檢測Cancel按鈕。

+0

我在哪裏添加空間? – user2860758

+0

輸入框的答案.... – tymeJV

+0

這工作我真的很感激它 – user2860758