“转到意外”错误一直显示,我找不到原因

时间:2014-12-31 00:10:47

标签: batch-file goto

echo Decode this next puzzle:
echo ymnx nx f kzs lfrj
echo Hint: The shift cipher is 5
set /p b=Answer: 
if %b%==%Answer: this is a fun game% goto correct2
if %b%==%?% goto oops
:correct2
echo hello!
pause

我一直收到goto意外错误,我检查了几个论坛,但找不到问题

2 个答案:

答案 0 :(得分:0)

常规IF语法为if [not] operand1==operand2 somethingtodo,其中operand1和operand2都是字符串。如果字符串包含分隔符,如 Space Tab 或其他对批处理具有特殊含义的字符,则字符串必须为"enclosed in quotes"

SET / P" var =提示"如果单独按 Enter ,则不会更改var。因此,如果var最初为空,则它仍为空。

如果您对if %var%==somevalue goto somewhere进行编码,那么如果var为空,则会将其解析为if ==somevalue goto somewhere。 "引用参数"因此:if "%var%"=="somevalue" goto somewhere缓解了这种情况。


if %b%==%Answer: this is a fun game% goto correct2
if %b%==%?% goto oops

应该是

if "%b%"=="Answer: this is a fun game" goto correct2
if "%b%"=="?" goto oops

(请注意,我还更正了第二个错误:%b%将访问变量b。在您的代码中,%Answer: this is a fun game%会尝试访问变量Answer: this is a fun game而不是文字字符串Answer: this is a fun game

(我将让你弄清楚要匹配的正确字符串是"这是一个有趣的游戏"不是"答案:这是一个有趣的游戏")

进一步提示:在您完成这两项更改后,您需要考虑如果您要输入?this is a fun game以外的内容会发生什么。在第二个if - :correct2只是一个标记后批处理只会继续执行下一个指令,批处理只将其用作gotocall的目标 - 所以下一个指令是echo hello!

答案 1 :(得分:0)

尝试这两行:空格破坏事物,通常用双引号处理 双引号还保护语句免受& < > |

等字符的影响
if /i "%b%"=="this is a fun game" goto correct2
if /i "%b%"=="?" goto oops