我是QBasic的新手和一般的编码,我制作的猜谜游戏刚刚起作用。
我必须制作一个不使用GOTO
或Do
语句的猜谜游戏,并为用户提供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
答案 0 :(得分:3)
你要求输入一次,然后你有一个闭环来检查答案,直到尝试大于4,但是尝试不会增加,因为Wend命令告诉它再次启动循环而不再问问题或者递增计数器。这就是所谓的“无限循环”,因为循环内的条件不会改变。我会留下它,看看你是否能弄清楚如何纠正这两个问题 - 请注意,只修复其中一个问题不会阻止它成为一个“无限循环”,你必须解决这两个问题。
答案 1 :(得分:1)
你的猜测必须在while循环中,当给出正确的答案时,%必须设置为等于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
答案 2 :(得分: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
答案 3 :(得分:0)
如果您仍然遇到问题我会在这里找到:
Input would you like to play again (yes/no)?", answer
...
if answer = "yes"
...
您必须更改答案以回答$,因为您无法将字符串保存在数字值内。
答案 4 :(得分: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