def initialize(letters)
@letters = letters
@face = letters.sample # letters is an array of all letters from A to Z
if @face == "Q"
@face = "Qu"
end
@visited = false
@coord = []
end
当我p
我的骰子后,我发现@face
仍然是“Q”
..., [#<Dice:0x007f907b032948 @letters=["H", "I", "M", "N", "Q", "U"], @face="Q", @visited=false, @coord=[]>, ...
发生了什么事?
答案 0 :(得分:0)
示例方法从数组中选择一个随机元素。我想你会发现,如果@letters等于[&#39; Q&#39;],你的代码就可以了。