ruby方法:if语句里面的if语句

时间:2011-10-05 12:23:35

标签: ruby syntax if-statement

我的程序中有很多错误,我的程序中没有/有太多“结束”标记。我之前测试过这段代码并且它有效但我只是想知道是否有人可以告诉我在这个if语句中是否有足够的“结束”或太多。感谢

def hop!(d)

if d== 0  
    if @current_location.addpoint(0,1) < @boundary1 
    puts "error"
    elsif if @current_location.addpoint(0,1) > @boundary2
    puts "error2"
    else
    @current_location= @current_location.addpoint(0,1) 
    puts "all good"
    end
    end
elsif d == 1
if @current_location.addpoint(0,-1) < @boundary1 
    puts "error"
    elsif if @current_location.addpoint(0,-1) > @boundary2
    puts "error2"
    else
    @current_location= @current_location.addpoint(0,-1) 
    puts "all good"
    end
    end
elsif d== 2
if @current_location.addpoint(1,0) <  @boundary1 
    puts "error"
    elsif if @current_location.addpoint(1,0) >  @boundary2
    puts "error2"
    else
    @current_location= @current_location.addpoint(1,0) 
    puts "all good"
    end
    end

else d= 3
if @current_location.addpoint(-1,0) <  @boundary1 
    puts "error"
    elsif if @current_location.addpoint(-1,0) >  @boundary2
    puts "error2"
    else
    @current_location= @current_location.addpoint(-1,0) 
    puts "all good"
    end
    end

end

4 个答案:

答案 0 :(得分:3)

使用支持自动重新注册的文本编辑器,您的代码如下所示:

if d== 0  
  if @current_location.addpoint(0,1) < @boundary1 
    puts "error"
  elsif if @current_location.addpoint(0,1) > @boundary2
    puts "error2"
  else
    @current_location= @current_location.addpoint(0,1) 
    puts "all good"
  end
end
elsif d == 1
  if @current_location.addpoint(0,-1) < @boundary1 
    puts "error"
  elsif if @current_location.addpoint(0,-1) > @boundary2
    puts "error2"
  else
    @current_location= @current_location.addpoint(0,-1) 
    puts "all good"
  end
end
elsif d== 2
  if @current_location.addpoint(1,0) <  @boundary1 
    puts "error"
  elsif if @current_location.addpoint(1,0) >  @boundary2
    puts "error2"
  else
    @current_location= @current_location.addpoint(1,0) 
    puts "all good"
  end
end
else d= 3
  if @current_location.addpoint(-1,0) <  @boundary1 
    puts "error"
  elsif if @current_location.addpoint(-1,0) >  @boundary2
    puts "error2"
  else
    @current_location= @current_location.addpoint(-1,0) 
    puts "all good"
  end
end

end

除了end的数量不正确之外,您似乎正在努力解决elsif问题,我想不出elsif if [...]对{{1}}有好处的任何情况写

答案 1 :(得分:3)

如果你正确地缩进代码就很清楚了。在下面,代码正确缩进,endelsif if的问题得到纠正:

def hop!(d)
  if d == 0  
    if @current_location.addpoint(0,1) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(0,1) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(0,1) 
      puts "all good"
    end
  elsif d == 1
    if @current_location.addpoint(0,-1) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(0,-1) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(0,-1) 
      puts "all good"
    end
  elsif d == 2
    if @current_location.addpoint(1,0) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(1,0) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(1,0) 
      puts "all good"
    end
  elsif d == 3
    if @current_location.addpoint(-1,0) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(-1,0) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(-1,0) 
      puts "all good"
    end
  end
end

此外,在这种情况下使用case会更优雅:

def hop!(d)
  case d
  when 0
    if @current_location.addpoint(0,1) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(0,1) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(0,1) 
      puts "all good"
    end
  when 1
    if @current_location.addpoint(0,-1) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(0,-1) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(0,-1) 
      puts "all good"
    end
  when 2
    if @current_location.addpoint(1,0) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(1,0) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(1,0) 
      puts "all good"
    end
  when 3
    if @current_location.addpoint(-1,0) < @boundary1 
      puts "error"
    elsif @current_location.addpoint(-1,0) > @boundary2
      puts "error2"
    else
      @current_location = @current_location.addpoint(-1,0) 
      puts "all good"
    end
  end
end

答案 2 :(得分:0)

case d
when 1:
  puts "error" and return if @current_location.addpoint(0,1) < @boundary1
  puts "error2" and return if  @current_location.addpoint(0,-1) > @boundary2
  puts "all good"
  break
when 2:
  puts "error" and return if @current_location.addpoint(1,0) < @boundary1 
  puts "error2" and return if @current_location.addpoint(1,0) >  @boundary2
  puts "all good"
  break
when 3:
  ...

我认为这应该可以解决问题。

答案 3 :(得分:0)

使用case

重写肯定会更好