开发者

Using array.delete(array.min) to delete one instance of a value

I'm trying to create a dice game program. I want to select one of the lowest out of five die, move it to a new array and remove the singular occurrence of that lowest dice from the original array. The problem is if there are more than one of the lowest die, this happens:

开发者_StackOverflow
array = %w[1,1,2,3,4]
new_array=[]
new_array.push(array.min) => 1
array.delete(array.min) => 2,3,4


new_array = array.sort.slice!(0..0)

or

new_array.push(array.sort!.shift)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜