我剛開始學習ruby。 現在我試着去編寫一個小腳本,其播放montyhall問題 我有一個問題與代碼一個小紅寶石腳本的問題
numgames = 10000 # Number of games to play
switch = true # Switch your guess?
wins = 0
numgames.times do doors = [0, 0, 0] # Three doors!
doors[rand(3)] = 1 # One of them has a car!
guess = doors.delete_at(rand(3)) # We pick one of them!
doors.delete_at(doors[0] == 0 ? 0 : 1) # Take out one of the remaining doors that is not a car!
wins += switch ? doors[0] : guess end
puts "You decided #{switch ? "" : "not "}to switch, and your win % is #{wins.times()/numgames}"
謝謝!但即時通訊仍然收到語法錯誤。 monty.rb:9:syntax error,unexpected')' ... nd你的勝利是#{wins.size()/ numgames}「 –
你的括號是否平衡? – miku
這是否意味着每一個缺口都被正確打開,關閉?然後是的! –