Ruby 用于查找多个返回值的脚本“;意外的$end“;

Ruby 用于查找多个返回值的脚本“;意外的$end“;,ruby,Ruby,有人知道为什么这个脚本不起作用吗 def multiples_three (n) i=1 while i<n if i%3==0 print "#{i}" i=i+1 elsif i=i+1 end 您缺少两个“结束”语句 一个是“如果” 另一个是“while”你错过了一个结尾 def multiples_three (n) i=1 while i<

有人知道为什么这个脚本不起作用吗

def multiples_three (n)
    i=1
    while i<n
        if i%3==0
            print "#{i}"
            i=i+1
        elsif
            i=i+1
end
您缺少两个“结束”语句

一个是“如果”


另一个是“while”

你错过了一个结尾

def multiples_three (n)
    i=1
    while i<n
        if i%3==0
            print "#{i}"
            i=i+1
        elsif
            i=i+1
        end #<--- needed
    end #<--- also needed
end
def乘以三(n)
i=1

而我看起来你少了几个“结尾”

def multiples_three (n)
    i=1
    while i<n
        if i%3==0
            print "#{i}"
            i=i+1
        elsif
            i=i+1
        end
    end
end
def乘以三(n)
i=1

而我正如其他人所说,你错过了一些结局。。。没有条件的赤裸的艾尔西夫让我讨厌;如果您没有实际测试某些东西,则需要另一个

至于样式,您应该在ruby中使用两个空格:

def multiples_three(n)
  i=1
  while i<n
    if i % 3 == 0
      print i  # no need to put in string and interpolate if it is by itself
      i=i+1
    else
      i=i+1
    end
  end
end

事实上,ruby让这一切变得如此简单,我甚至都不会编写这个函数。

非常感谢!!事实上,我尝试了两个“结束”,但这一个需要三个,我完全错过了def的结束!!!Ruby不是Python。如下所述,您缺少
if
块的结束
end
语句。此外,在风格上,Ruby代码使用两个空格作为缩进,而不是四个或八个空格,而且绝对不是制表符。如果您的代码永远不会传递给其他开发人员,您可以使用另一个标准,但如果您在我工作的小组中没有遵循这一标准,您会很快听到这一消息,紧接着会发出嚎叫和咬牙切齿的声音。没有条件的elsif也是一个问题
def multiples_three(n)
  i=1
  while i<n
    if i % 3 == 0
      print i  # no need to put in string and interpolate if it is by itself
      i=i+1
    else
      i=i+1
    end
  end
end
def multiples_three(n)
  (1..n).select{|i| i % 3==0 }
end

puts multiples_three(12).join("\n")
3
6
9
12
=> nil