繁体   English   中英

错误-“此时Goto出乎意料”,当留空字段并按Enter时-批处理

[英]Error - “goto was unexpected at this time” When leaving a blank field and just hitting enter - Batch

问题是运行游戏时,如果我将该字段留空,然后按Enter,则窗口将关闭。
在目录的“命令提示符”窗口中运行游戏(以使其停止关闭)时,我看到上述错误消息。
如果输入了字符,则“无效选择”“转到开始”效果很好,这都与空白字段有关。

我有:

  1. 尝试if not defined goto
  2. 尝试将变量包含在“”中

但是似乎没有任何效果!

: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

如果用户输入空白, 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

哪个应该工作。

莫娜

作为一个额外的建议,使用/ i不区分大小写的开关,可以用后面的两行替换这些行,而大小写无关紧要。

如果“%bug%” ==“在”转到bugin
如果“%bug%” ==“ Out”转到bugout
如果“%bug%” ==“ in”转到Bugin
如果“%bug%” ==“ out”转到bugout

if /i "%bug%"=="In" goto bugin
if /i "%bug%"=="Out" goto bugout

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM