我正在嘗試編寫一個Ruby程序來處理2張卡片給一個玩家,但是我得到錯誤消息,沒有將Fixnum隱式轉換爲字符串。我看過類似的問題,我不認爲它們適用於這個問題。我將不勝感激任何幫助。這裏是我的代碼:紅寶石 - 沒有將Fixnum隱式轉換爲字符串
def get_card (card)
suit = (card - 1)/13
if (card == 0)
puts " of clubs"
elsif (card == 1)
puts " of diamonds"
elsif (card == 2)
puts " of hearts"
elsif (card == 3)
puts " of spades"
end
card % 13
if (card == 1)
return "ace" + suit
elsif (card == 11)
return "jack" + suit
elsif (card == 0)
return "king" + suit
elsif (card == 2)
return "2" + suit
elsif (card == 3)
return "3" + suit
elsif (card == 4)
return "4" + suit
elsif (card == 5)
return "5" + suit
elsif (card == 6)
return "6" + suit
elsif (card == 7)
return "7" + suit
elsif (card == 8)
return "8" + suit
elsif (card == 9)
return "9" + suit
elsif (card == 10)
return "10" + suit
elsif (card == 12)
return "queen" + suit
end
end
def deal_cards
total_cards = (1..52).to_a.shuffle
player_value = [total_cards.pop, total_cards.pop]
puts "Your cards are " + get_card(player_value[0]).to_s + " and " + get_card(player_value[1]).to_s
end
deal_cards
你能減少這個例子t o一個非常小的東西?應該可以創建一行或一行代碼,只顯示一行或兩行代碼,這就表明了這個問題。 –