这个 "Guess my number" 批量游戏有什么问题?
What's wrong with this "Guess my number" batch game?
我写了这个游戏。它只是在 1 到 16 之间设置一个随机数,用户必须猜出它。当猜测正确时,它会向用户显示尝试次数。
但是当我运行它并输入一个数字时,它会显示错误并立即关闭。我认为错误说缺少一些东西。
@echo off
color 0A
set /a key = %random% / 2048
set /a attempts=0
title Guess My Number (0 to 16)
:AGAIN
set /p in = Guess it.
set /a in = %in%
if %key% GTR %in% (
echo My Number is greater.
set /a attempts=%attempts%+1
goto again
)
if %key% LSS %in% (
echo My Number is less.
set /a attempts=%attempts%+1
goto again
)
if %key% == %in% (
echo right!
echo You Guessed it in %attempts% attempts.
goto end
)
:end
pause
正如 npocmaka 所写,错误是你的 SET
语句中有 spacec。删除它们会导致以下代码完美运行:
@echo off
color 0A
set /a key =%random% / 2048
set /a attempts=0
title Guess My Number (0 to 16)
:AGAIN
set /p in=Guess it.
set /a in=%in%
if %key% GTR %in% (
echo My Number is greater.
set /a attempts=%attempts%+1
goto again
)
if %key% LSS %in% (
echo My Number is less.
set /a attempts=%attempts%+1
goto again
)
if %key%==%in% (
echo right!
echo You Guessed it in %attempts% attempts.
goto end
)
:end
pause
编辑:顺便说一下,你的代码是胡说八道。答案总是 2。:D 我猜你需要像 set /a key=%random%%%16
这样的东西
这就是我如何做你的小游戏
@echo off
color 0A
title Guess My Number (0 to 16)
:Start
set /a key=%random%%%16
set attempts=0
:again
set /p in=What is your guess? (0-16):
set /a attempts+=1
if %key% GTR %in% (
echo My Number is greater.
goto again
)
if %key% LSS %in% (
echo My Number is less.
goto again
)
if %key% == %in% (
echo CORRECT!
echo You Guessed it in %attempts% attempts.
pause
)
您的问题已经解决,增加了额外的空间。但是我使用了不同的方法来生成最多 16 的随机数,并且我将您的尝试计数器移到了 set /p
的正下方,因此它总是会向上计数。在您当前的版本中,您将其设置为仅针对不正确的猜测。我认为它也应该算上最后的猜测。另外,我为您稍微缩短了尝试次数,这对您以后很有帮助。
-Edit- 我忘了说,你根本不需要这一行 set /a in=%in%
因为变量 %in%
已经设置好了第 set /p in=Guess it.
行无需设置两次。
我写了这个游戏。它只是在 1 到 16 之间设置一个随机数,用户必须猜出它。当猜测正确时,它会向用户显示尝试次数。
但是当我运行它并输入一个数字时,它会显示错误并立即关闭。我认为错误说缺少一些东西。
@echo off
color 0A
set /a key = %random% / 2048
set /a attempts=0
title Guess My Number (0 to 16)
:AGAIN
set /p in = Guess it.
set /a in = %in%
if %key% GTR %in% (
echo My Number is greater.
set /a attempts=%attempts%+1
goto again
)
if %key% LSS %in% (
echo My Number is less.
set /a attempts=%attempts%+1
goto again
)
if %key% == %in% (
echo right!
echo You Guessed it in %attempts% attempts.
goto end
)
:end
pause
正如 npocmaka 所写,错误是你的 SET
语句中有 spacec。删除它们会导致以下代码完美运行:
@echo off
color 0A
set /a key =%random% / 2048
set /a attempts=0
title Guess My Number (0 to 16)
:AGAIN
set /p in=Guess it.
set /a in=%in%
if %key% GTR %in% (
echo My Number is greater.
set /a attempts=%attempts%+1
goto again
)
if %key% LSS %in% (
echo My Number is less.
set /a attempts=%attempts%+1
goto again
)
if %key%==%in% (
echo right!
echo You Guessed it in %attempts% attempts.
goto end
)
:end
pause
编辑:顺便说一下,你的代码是胡说八道。答案总是 2。:D 我猜你需要像 set /a key=%random%%%16
这就是我如何做你的小游戏
@echo off
color 0A
title Guess My Number (0 to 16)
:Start
set /a key=%random%%%16
set attempts=0
:again
set /p in=What is your guess? (0-16):
set /a attempts+=1
if %key% GTR %in% (
echo My Number is greater.
goto again
)
if %key% LSS %in% (
echo My Number is less.
goto again
)
if %key% == %in% (
echo CORRECT!
echo You Guessed it in %attempts% attempts.
pause
)
您的问题已经解决,增加了额外的空间。但是我使用了不同的方法来生成最多 16 的随机数,并且我将您的尝试计数器移到了 set /p
的正下方,因此它总是会向上计数。在您当前的版本中,您将其设置为仅针对不正确的猜测。我认为它也应该算上最后的猜测。另外,我为您稍微缩短了尝试次数,这对您以后很有帮助。
-Edit- 我忘了说,你根本不需要这一行 set /a in=%in%
因为变量 %in%
已经设置好了第 set /p in=Guess it.
行无需设置两次。