要计算$ totalNoOfGuesses你会在每场比赛中加上$ noOfGuesses,对吗?我如何在我的代码中执行它?我尝试了几种不同的选择,但它没有用。我应该创建一个数组还是什么?
def play_game
$noOfGuesses=0
$gameCount+=1
number = generate_number
loop do
print "\nEnter your guess and press the Enter key: "
reply = STDIN.gets
reply.chop!
reply = reply.to_i
$noOfGuesses+=1
在此之间,如果回复>或者<,太高或太低... reply = get.to_i
$noOfGuesses=0
$gameCount=0
$totalNoOfGuesses=0
$avgNoOfGuesses=0
answer = ""
loop do
Console_Screen.cls
print "Are you ready to play the Ruby Number Guessing Game? (y/n): "
answer = STDIN.gets
answer.chop!
break if answer == "y" || answer == "n"
end
if answer == "n"
Console_Screen.cls
puts "Okay, perhaps another time.\n\n"
else
loop do
SQ.play_game
Console_Screen.cls
print "It took you #{$noOfGuesses} attempt#{'s' if $noOfGuesses > 1}.\n"
print "You have played #{$gameCount} time#{'s' if $gameCount > 1}.\n"
print "It has taken you #{$totalNoOfGuesses} attempts in #{$gameCount} game#{'s' if $gameCount >1}.\n\n"