我明白了:
Untitled 5.rb:16: syntax error, unexpected end-of-input, expecting
keyword_end
即使我使用if
关闭了else
/ def
语句和end
:
def max_2_sum(int_collection)
sum = 0
if int_collection.empty?
sum
else if int_collection.count == 1
int_collection.first
else
sum = int_collection.sort[-1] + int_collection.sort[-2]
end
end
int_collection = [4,3,9]
puts max_2_sum(int_collection)
答案 0 :(得分:2)
else if
错了。它应该是
elsif
答案 1 :(得分:0)
您的代码应如下所示
def max_2_sum(int_collection)
sum = 0
if int_collection.empty?
sum
elsif int_collection.count == 1
int_collection.first
else
sum = int_collection.sort[-1] + int_collection.sort[-2]
end
end