我创建了一个简单的游戏,用户必须按字母顺序排列单词的顺序才能得分。由于一些奇怪的原因,即使用户得到2个问题,得分也会返回1。我究竟做错了什么?
def alphabetize(word)
word.chars.sort_by(&:downcase).join
end
def words
%w(hello yes)
end
@correct = 0
@incorrect = 0
def score
(@correct / @correct + @incorrect)
end
words.each do |word|
puts "Alphabetize '#{word}'"
answer = gets.chomp
if answer == alphabetize(word)
@correct += 1
p 'Nice!'
else
@incorrect += 1
p 'You suck!'
end
end
p "Your score: #{score}"
答案 0 :(得分:2)
,因为:
@correct / @correct
总是1