批处理中的选择命令不起作用

时间:2021-08-22 02:07:32

Okay, so I'm having some trouble using errorlevel and the choice command, and frankly I have no idea what's going on.

好的,所以我在使用errorlevel和choice命令时遇到了一些麻烦,坦率地说我不知道​​发生了什么。

I've been using this code:

我一直在使用这段代码:

:CACD  
set stage=CACD  
echo.  
echo Make a choice  
echo.  
echo 1)  
echo 2)    
echo 3)  
echo.  
choice /c 7034 /n  
if %errorlevel% == "3" goto choice3  
if %errorlevel% == "2" goto se2  
if %errorlevel% == "1" goto choice1  
goto CACD  

:choice3  
echo you chose 3  
pause  
goto CACD  

:se2  
echo you chose 2  
pause  
goto CACD  

:choice1  
echo you chose 1  
goto CACD

Whenever I enter 1, nothing happens. Same thing with 2. But whenever I enter 3, it works? Can anyone help?

每当我输入1时,没有任何反应。同样的事情2.但每当我输入3,它的作品?有人可以帮忙吗?

1 个解决方案

#1


1  

Since choice limits your input, errorlevel can only be one of 1,2,3,255

由于选择限制了您的输入,因此errorlevel只能是1,2,3,255之一

You can omit all the if commands if you append the errorlevel to your goto label: and name all the labels accordingly.

如果将errorlevel附加到goto标签,则可以省略所有if命令:并相应地命名所有标签。

@Echo off
:CACD  
set stage=CACD  
echo.  
echo Make a choice  
echo.  
echo 1)  
echo 2)    
echo 3)  
echo.  
choice /c 123 /n  
goto choice%errorlevel%
:Choice255
Echo an error occured with your choice
goto :Eof  

:choice3  
echo you chose 3  
pause  
goto CACD  

:choice2  
echo you chose 2  
pause  
goto CACD  

:choice1  
echo you chose 1  
goto CACD

#1


1  

Since choice limits your input, errorlevel can only be one of 1,2,3,255

由于选择限制了您的输入,因此errorlevel只能是1,2,3,255之一

You can omit all the if commands if you append the errorlevel to your goto label: and name all the labels accordingly.

如果将errorlevel附加到goto标签,则可以省略所有if命令:并相应地命名所有标签。

@Echo off
:CACD  
set stage=CACD  
echo.  
echo Make a choice  
echo.  
echo 1)  
echo 2)    
echo 3)  
echo.  
choice /c 123 /n  
goto choice%errorlevel%
:Choice255
Echo an error occured with your choice
goto :Eof  

:choice3  
echo you chose 3  
pause  
goto CACD  

:choice2  
echo you chose 2  
pause  
goto CACD  

:choice1  
echo you chose 1  
goto CACD