[英]Visual Basic 2012: Immediately Exiting while Loop
我正在為Visual Basic開發一個控制台應用程序,它將在while循環中運行一段代碼。 如果按下退出鍵,我希望while循環在任何時候立即停止。
我不確定如何讓程序連續監聽逃逸按鍵,而不必輸入一百萬個檢查點。 我意識到在下面的示例(只是我為示例編寫的內容)中,在“ readline”檢查期間,我有機會更改值。 任何幫助,將不勝感激!
Dim inp As Char
Dim x, y, z As Integer
While inp <> Chr(27)
console.writeline ("Press escape at any time to exit")
console.writeline ("Please enter a numeric value: ")
x = console.readline()
If x > 1 Then
console.writeline ("greater")
Else
console.writeline ("lesser")
End If
console.writeline ("Do you have other values? Y or N")
inp = console.readline()
If inp = "y" Then
'...other stuff
Else
console.writeline ("Press escape to exit or any other key to continue")
inp = console.readline()
End If
End While
也許您還可以定義一個公共布爾變量,並在使用鍵盤按下事件的轉義鍵時將其置於true。 在循環中,您可以在某個戰略要點檢查此布爾值,並判斷為真。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.