1

我是 QBasic 和一般编码的新手,我正在制作一个无法正常工作的猜谜游戏。我要做一个不使用GOTOorDo语句的猜谜游戏,给用户5次机会。这是代码:

chances%=1
dim guess as integer
dim answer as string
randomize timer
rndnum=INT(RND*100+1)

'makinng a title
color 5
locate 12,32
print "welcome to My guessing game."
Print "think of a number between 1 and 100."

color 12
Input "enter you guess:  ",guess
while chances%<4
if guess >rndnum then 
  print "wrong, too high"
elseif guess <rndnum then 
   print "wrong, too low"
elseif guess=rndnum then
 print "your guessed the number!"
end if 
wend
chances%=chances%+1

color 14
Print "you took "; chances%;"to guess the number"

color 3
Input would you like to play again (yes/no)?", answer
wend 

if answer = "yes" then
?

else 
  print "have a good day"
end if 
end 
4

5 回答 5

3

您要求输入一次,然后您有一个闭环检查答案,直到尝试大于四,但尝试不会增加,因为 Wend 命令告诉它再次启动循环而不再次询问问题或增加计数器。这就是所谓的“无限循环”,因为循环内的条件不会改变。我将把它留在那里,看看你是否能弄清楚如何纠正这两个问题 - 请注意,只修复其中一个不会阻止它成为“无限循环”,你必须同时解决这两个问题。

于 2015-03-03T21:06:38.850 回答
1

您可以使用WHILE...WEND循环运行,直到机会变为 0。这就是我的意思:

....(rest of code)
chances = 5
WHILE chances > 0 
    ....
    if guess > rndnum then 
       print "wrong, too high"
       chances = chances - 1
    elseif guess < rndnum then 
       print "wrong, too low"
       chances = chances -1
     ....
WEND
于 2017-06-08T11:45:54.600 回答
1

您的猜测必须在 while wend 循环内,并且当给出正确答案时,必须将 chance% 设置为等于 4,否则您将进入一个永恒循环。也有必要在第一次猜测后直接增加几率。请参阅稍微更改的代码。另请查看猜测并更改您的行,说您将 x 猜测从几率百分比更改为猜测

chances%=0
while chances% < 4
    Input "enter your guess:  ",guess
    chances% = chances% + 1
    if guess > rndnum then 
        print "wrong, too high"
    elseif guess < rndnum then 
        print "wrong, too low"
    elseif guess = rndnum then
        print "your guessed the number!"
        guesses = Chances%
        chances% = 4
    end if 
wend
于 2015-09-12T09:01:47.817 回答
0

此片段演示了 QB64 中的数字猜谜游戏:

REM guessing game.
Tries = 5
DO
    PRINT "Guess a number between 1 and 100 in"; Tries; "guesses."
    Number = INT(RND * 100 + 1)
    Count = 1
    DO
        PRINT "Enter guess number"; Count; " ";
        INPUT Guess
        IF Guess = Number THEN
            PRINT "Correct! You guessed it in"; Count; "tries."
            EXIT DO
        END IF
        IF Guess > Number THEN
            PRINT "Wrong. Too high."
        ELSE
            PRINT "Wrong. Too low."
        END IF
        Count = Count + 1
        IF Count > Tries THEN
            PRINT "The number was"; Number
            PRINT "You didn't guess it in"; Tries; "tries."
            EXIT DO
        END IF
    LOOP
    DO
        PRINT "Play again(yes/no)";
        INPUT Answer$
        IF LCASE$(Answer$) = "no" THEN
            END
        END IF
        IF LCASE$(Answer$) = "yes" THEN
            EXIT DO
        END IF
    LOOP
LOOP
END
于 2017-09-12T01:13:27.150 回答
0

如果您仍然遇到问题,我在这里发现:

Input would you like to play again (yes/no)?", answer

...

if answer = "yes"

...

您必须将答案更改为 answer$,因为您无法将字符串保存在数字值中。

于 2017-07-18T00:34:22.137 回答