Ruby Koan 151 raising exceptions

后端 未结 30 1627
孤独总比滥情好
孤独总比滥情好 2021-01-31 14:34

I\'m going through the ruby koans, I\'m on 151 and I just hit a brick wall.

Here is the koan:

# You need to write the triangle method in the file \'trian         


        
30条回答
  •  醉酒成梦
    2021-01-31 14:58

    No Need to change the TriangleError code for either challenge. You just need to check for invalid triangles and raise the error if the triangle isn't.

    def triangle(a, b, c)
      if a==0 && b==0 && c==0
        raise TriangleError, "This isn't a triangle"
      end
      if a <0 or b < 0 or c <0
        raise TriangleError, "Negative length - thats not right"
      end
      if a + b <= c  or a + c <= b or  b + c <= a
        raise TriangleError, "One length can't be more (or the same as) than the other two added together.  If it was the same, the whole thing would be a line.  If more, it wouldn't reach. "
      end    
      # WRITE THIS CODE
      if a == b and b == c 
        return :equilateral    
      end      
      if (a==b or b == c or a == c) 
        return :isosceles
      end 
      :scalene      
    end
    

提交回复
热议问题