问题是在运行游戏时,如果我将该字段留空并且只需按Enter键,窗口就会关闭 在目录中的命令提示符窗口中运行游戏时(为了停止关闭),我看到上面的错误信息 如果输入一个字符,“无效选择”'转到开始'工作正常,这一切都与空白字段有关。
我有:
if not defined
goto
但似乎没有任何作用!
:start
cls
echo You need to decide NOW, are you going to barricade yourself in (Bug in),
echo or go to somewhere out of town (Bug out)?
echo.
set /p bug=I want to bug
if %bug%==In goto bugin
if %bug%==Out goto bugout
if %bug%==in goto bugin
if %bug%==out goto bugout
echo.
echo Invalid selection
pause
cls
goto start
答案 0 :(得分:0)
如果用户输入空白,if
语句将失败:
set var=
if %var%==blah Echo BLAH
Rem The above results in :: if ==blah Echo BLAH :: Which will crash CMD
set var=
if "%var%"=="blah" Echo BLAH
Rem The above results in :: if ""=="blah" Echo BLAH :: Which will result in false
因此,在引号中包含语句将导致:
:start
cls
echo You need to decide NOW, are you going to barricade yourself in (Bug in),
echo or go to somewhere out of town (Bug out)?
echo.
set /p bug=I want to bug
if "%bug%"=="In" goto bugin
if "%bug%"=="Out" goto bugout
if "%bug%"=="in" goto bugin
if "%bug%"=="out" goto bugout
echo.
echo Invalid selection
pause
cls
goto start
哪个应该有用。
莫纳
答案 1 :(得分:0)
作为一个额外的建议,使用/ i不区分大小写的开关,这些行可以替换为它们后面的两行,并且大小写无关紧要。
if“%bug%”==“In”goto bugin
if“%bug%”==“Out”goto bugout
if“%bug%”==“in”goto bugin
if“%bug%”==“out”goto bugout
if /i "%bug%"=="In" goto bugin
if /i "%bug%"=="Out" goto bugout