if语句后批处理如何使用括号?

时间:2013-01-28 23:56:27

标签: if-statement batch-file

我正在尝试用批量创建一个简单的数字猜谜游戏。但是,我从来没有能够正确使用括号。代码是:

@echo off
::This is a game that has you guess a random number.
Title Random Number Game
set number=%random%
:guess
set /p guess1= Guess the number I'm thinking of!
if %guess1%LSS%number% (
echo Higher!
goto guess
)
if %guess1%GTR%number% (
echo Lower!
goto guess
)
echo You got it! Awesome Job!

无论我的猜测是高于还是低于数字,在输入之后,cmd都会返回,“(此时并未预料到。”我如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

你几乎把一切都搞定了。你只需要在比较运算符周围有一个空格

if %guess1% LSS %number% (
  echo Higher!
  goto guess
)
if %guess1% GTR %number% (
  echo Lower!
  goto guess
)