batch-file - Guessing game with only 10 tries in batch
问题描述
I'm trying to make this guessing game with only 10 tries in batch but whatever number I guess it says the number is smaller and I can't find the problem
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
解决方案
Maybe you have missed the code:
SETLOCAL ENABLEDELAYEDEXPANSION
.And while comparing the size of the number, it is not necessary to use quotes.
Or if you don't wish to use it, you can make a more complicated loop.
@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
推荐阅读
- azure - 使用 Arm 模板重新生成存储帐户密钥
- hadoop - 哪个大数据搜索引擎
- multithreading - TypeError:字符串格式化期间并非所有参数都转换 - opc ua客户端到数据库连接
- java - 如何通过参数从 Android 中的 Firebase 数据库中获取数据?
- python - 为什么scipy的熵函数返回的值大于1?
- c# - 方法名称与类相同,是什么意思?
- python - 插槽在哪个线程中执行,我可以将其重定向到另一个线程吗?
- sql - SQL Server 在文本列中搜索确切的短语
- javascript - Angular:如何将 *ngFor 与父子组件一起使用
- python - 从新列中的起点有条件地递增日期时间差为整数