无法计算语法错误,意外的输入结束,期望keyword_end错误

时间:2014-11-13 04:03:52

标签: ruby

我明白了:

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)

2 个答案:

答案 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