2012-06-18 57 views
4

我做這樣的事情:選擇和錯誤級別?

echo 1-exit 
echo 2-about 
echo 3-play 
choice /c 123 >nul 
if errorlevel 1 goto exit 
if errorlevel 2 goto about 
if errorlevel 3 goto play 
:play 
blah 
:about 
blah 
:exit 
cls 

如果我選擇「播放」選項,它退出。我如何防止這種情況發生?

回答

6

如果選擇返回的實際錯誤級別大於或等於給定值,if errorlevel表達式的計算結果爲true。所以如果你打3,第一個if表達式是真的,腳本終止。請致電help if瞭解更多信息。

有兩種簡單的解決方法。

第一個(更好) - 替換if errorlevel表達與具有給定值%ERRORLEVEL%系統變量的實際的對比:

if "%ERRORLEVEL%" == "1" goto exit 
if "%ERRORLEVEL%" == "2" goto about 
if "%ERRORLEVEL%" == "3" goto play 

第二個 - comparisions的變化順序:

if errorlevel 3 goto play 
if errorlevel 2 goto about 
if errorlevel 1 goto exit 
1

最簡單的方法解決這個問題的方法是用%errorlevel%的值直接去所需的標籤:

echo 1-exit 
echo 2-about 
echo 3-play 
choice /c 123 >nul 
goto option-%errorlevel% 
:option-1 
rem play 
blah 
:option-2 
rem about 
blah 
:option-3 
exit 
cls