一個批次檔中的 2 個選擇指令混淆了

一個批次檔中的 2 個選擇指令混淆了

例子:

@echo off
goto menu1

:menu1
cls
echo Menu 1
echo.
echo Press "1" to start control panel
echo.
echo Press "2" to go to second menu
echo.
choice /c 12
if errorlevel ==2 goto menu2
if errorlevel ==1 goto control panel


:menu2
cls
echo Menu 2
echo.
echo Press "1" start msconfig
echo.
echo Press "2" to go to first menu
echo.
choice /c 12
if errorlevel ==2 goto menu1
if errorlevel ==1 goto msconfig


:control panel
cls
start control
goto menu1


:msconfig
cls
start msconfig
goto menu1

所以如果我在選單1我按 2 它將轉到選單2

如果我在選單2我按 2 它將轉到選單1,

但如果我在選單2我按 1 開啟 msconfig,它改為開啟控制台選單1儘管選單2還是打開了,這是為什麼呢?

答案1

為此,您可以(應該)使用 if 命令而不是錯誤等級。您還需要將使用者輸入設定為這樣的變量, set /p examplemenu1= Enter your choice^> 這會將使用者選擇的內容插入到 examplemenu1 中,然後使用 if 指令。

我將向您展示一種更好的方法來編寫菜單並解決您的問題。在此選單中,如果有人不輸入任何內容,則不會發生任何事情。

@echo off
:start
:menu1
cls
echo.
echo.
echo Enter 1 and Press Enter to open control panel
echo.
echo Enter 2 and Press Enter to go menu 2
echo.
set /p menu1= Enter your choice then Press Enter^>
if "%menu1%" EQU "1" goto :open-control
if "%menu1%" EQU "2" goto :menu2
goto :menu1

:menu2
Cls
REM I will do the basics
echo Menu 2
echo Type 1 to open msconfig
echo.
echo Type 2 to go back to menu 1
echo.
set /p menu2= Enter your choice and press enter^>
if "%menu2%" EQU "1" goto :open-msconfig
if "%menu2%" EQU "2" goto :menu1
goto :menu2

:open-control
cls
start control
REM People will ping a invalid host to wait
REM But they don't know that you can just use localhost
ping localhost -n 3 >nul
goto :start

:open-msconfig
Cls
start msconfig
ping localhost -n 3 >nul
goto :start

測試一下並回覆它是否適合您。

相關內容