0

我想出了以下针对公案的解决方案。

# and
#   about_triangle_project_2.rb
#
def triangle(a, b, c)

   driehoek = Array.new[ a, b, c].sort

  raise (TriangleError), "length cannnot be 0 or lesser" if (driehoek[0] <= 0) 
  raise (TriangleError), "impossible triangle" if (driehoek[0] + driehoek[1] < driehoek[2])

 return :equilateral if ((a == b) and (b == c))
 return :isosceles if (((a == b) and (b != c)) or 
                   ((a != b) and (b == c)) or 
                   ((a == c) and (a != b)))
  return :scalene  if ((a !=b) and (b != c))
end

# Error class used in part 2.  No need to change this code.
class TriangleError < StandardError
end

但是现在当使用三角形 [2,2,2] 时,我会看到以下错误消息:

The answers you seek...
 wrong number of arguments (3 for 2)

Please meditate on the following code:
  ./triangle.rb:18:in `[]'
  ./triangle.rb:18:in `triangle'

谁能告诉我这里有什么问题?

罗洛夫

4

1 回答 1

1

问题在于您如何创建阵列。new是一种方法,您需要使用parens (). 改变这个:

driehoek = Array.new[ a, b, c].sort

为此,它应该可以工作:

driehoek = Array.new([a, b, c]).sort
于 2012-09-23T08:31:17.610 回答