只能进行10次尝试的猜猜游戏

时间:2018-10-19 15:04:07

标签: batch-file

我正在尝试仅通过10次尝试来制作这个猜谜游戏,但是无论我猜有多少数字,它都表明数字较小并且我找不到问题

set /a var=%random%
echo %var%
for /l %%x in (1, 1, 10) do (

set /p guess="Try to guess the number: "
if "%guess%" equ "%var%" (goto 1)
if "%guess%" gtr "%var%" (echo Your number is greater)
if "%guess%" lss "%var%" (echo Your number is smaller)

)

echo You lost
pause
exit
:1
echo you guessed the number

1 个答案:

答案 0 :(得分:0)

  

也许您错过了代码:SETLOCAL ENABLEDELAYEDEXPANSION

     

在比较数字大小时,没有必要使用引号。


或者,如果您不想使用它,可以进行更复杂的循环。

@echo off
set /a var=%random%
rem I don't know why you would add the next line but I'll still let it stay there.
echo %var%
set l=0
goto guess

:guess
set /a l=l+1
set guess=-1
set /p guess="Try to guess the number: "
if %guess% equ %var% (goto correct)
if %guess% lss 0 (echo The input should be an integer which is not less than 0)
if %guess% gtr %var% (echo Your number is greater) else (echo Your number is smaller)
if "%l%" == "10" (goto fail) else (goto guess)

:fail
echo You lost
pause
exit

:correct
echo you guessed the number
pause
exit