【发布时间】:2018-05-28 13:03:07
【问题描述】:
我正在尝试检查一个数字是否是一个完美的正方形。
这是我的代码:
def is_square(x)
return true if
math.sqrt(x).is_a? Integer
end
知道为什么它不起作用吗?
【问题讨论】:
-
Math应该大写,例如return true if Math.sqrt(x).is_a?(Integer) 。此外,方法的名称最好是is_square?(x) -
谢谢。我刚刚这样做了,但它仍然返回以下错误消息:# main.rb:4:in
is_square' main.rb:11:inblock (2 levels) in ' /runner/frameworks/ruby/cw-2.rb:180:in @987654327 @it' /runner/frameworks/ruby/cw-2.rb:206:init' main.rb:10:inblock in ' /runner/frameworks/ruby/cw-2.rb:55:inblock in describe' /runner/frameworks/ruby/cw-2.rb:46:inmeasure' /runner/frameworks /ruby/cw-2.rb:51:indescribe' /runner/frameworks/ruby/cw-2.rb:202:indescribe' main.rb:9:in ` ' -
你用什么来运行程序?
-
它在代码战上。即使我除以 1 以避免浮动,它仍然充满错误......别担心,你的回答仍然非常有用,帮助我了解更多关于编码的知识!
标签: ruby