【问题标题】:How to use multiple answers for the same IF statement如何对同一个 IF 语句使用多个答案
【发布时间】:2014-08-18 13:26:24
【问题描述】:

我使用批处理文件在 CMD 上创建了一个简单的谜语程序。我希望它也可以回答诸如 1 之类的答案。到目前为止,我有这个:

:Riddle_1
cls
echo.
echo I shake the earth with booming thunder
echo fell forests whole and homes complete.
echo I influence ships, topple kings
echo sweep down swift yet remain unseen.
echo.
set /p answer=What am I?
if %answer%==wind (goto Riddle_2) else (goto Riddle_1)

当我尝试时:

if %answer%==wind (goto Riddle_2) else (goto Riddle_1)
if %answer%==Wind (goto Riddle_2) else (goto Riddle_1)

它完全忽略第二个命令,其他变体会这样做,否则它们将忽略 else 命令并在答案错误时将其转到下一个问题。

有没有办法解决这个问题?

【问题讨论】:

标签: batch-file cmd


【解决方案1】:

你可以这样做

set res=false
if %answer%==wind set res=true
if %answer%==Wind set res=true
if "%res%"=="true" (
    goto Riddle_2
)
else
(
   goto Riddle_1
)

【讨论】:

  • foxidrive 的回答比较好!
【解决方案2】:

/i 使比较不区分大小写,双引号使其在各种方面更加健壮。

if /i "%answer%"=="wind" (goto Riddle_2) else (goto Riddle_1)

【讨论】:

    猜你喜欢
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多