带有errorlevel的批处理选择命令不起作用

col*_*non 2 batch-file

我对选择命令感到困惑.这是我的代码:

@echo off
:start
cls
echo yes or no?
Choice/c yn
if errorlevel 1 goto yes
if errorlevel 2 goto no
:yes
echo you pressed yes
pause
goto start
:no
echo you pressed no
pause
goto start
Run Code Online (Sandbox Code Playgroud)

问题是我每次都是肯定的.我想出我是否用过这个:

set x=%errorlevel%
Run Code Online (Sandbox Code Playgroud)

然后用

if %x%==1 goto yes
if %x%==2 goto no
Run Code Online (Sandbox Code Playgroud)

并且脚本工作正常.为什么是这样?我想我记得读过一些关于检查errorlevel实际上可能设置一个新的错误级别,如果是false,或类似的东西.一点帮助?

MC *_* ND 6

该构造if errorlevel n检查errorlevel是否至少为n.所以,如果ERRORLEVEL为4,则测试if errorlevel 1if errorlevel 4,所有的人,返回true.

进行测试的方法是从较高的errorlevel到较低的errorlevel

if errorlevel 2 goto no
if errorlevel 1 goto yes
Run Code Online (Sandbox Code Playgroud)