我想寫一個硬幣翻轉程序,我可以分析頭翻轉的百分比。我已經開始投幣工作,而不是實際分析。硬幣翻轉統計程序 - 未定義的方法
問題是當我創建一個Coin類以便進一步將對象分解成類似於Coin.length之類的對象時。
爲什麼我得到一個"undefined method 'flip' for Coin:Class (NoMethodError)" from flip.rb:14:in 'times' from flip.rb:14:in <main>
當我確實有一個?
class Coin
def flip
flip = 1 + rand(2)
if flip == 2
then puts "Heads"
else
puts "Tails"
end
end
end
10.times do
Coin.flip
end
下面是我有點想模仿模切輥例如:
class Die
def roll
1 + rand(6)
end
end
# Let's make a couple of dice...
dice = [Die.new, Die.new]
# ...and roll them.
dice.each do |die|
puts die.roll
end