这有什么问题"猜猜我的号码"批量游戏?

时间:2015-10-22 07:47:48

标签: batch-file cmd command-prompt

我写了这个游戏。它只是设置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

2 个答案:

答案 0 :(得分:2)

正如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

这样的东西

答案 1 :(得分:-1)

这是我如何做你的小游戏

@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%已设置为此行{{ 1}}没有必要设置两次。