2011-10-05 16 views
0

私のプログラムで、私のプログラムに「終了」マーカーが多すぎる/していないというエラーが多かったです。私は前にこのコードをテストしていますが、動作しますが、if文またはtoo manyに十分な "end"があるかどうかを誰かが教えてくれるかどうかは疑問です。 !おかげruby​​ method:ifステートメント内のステートメント

はデフホップ(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 
+1

Rubyの規約に従えば、この問題に直面することはありません。 – Bohdan

+0

"if-in-if"条件の結果を変数に代入することで、読み込みを簡単にすることができます。あなたのコードに大きな利益をもたらすでしょう! – Romain

答えて

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 [...]は書くのに良いことです

3

あなたがコードを正しくインデントしているかどうかは明らかです。次のコードでは、適切にインデントされるとend sの問題とelsif 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 
+0

Hmm ..何もクリアされていません:) OPのコードでは、 'puts"の後に2つの 'end'sがありますが、あなたのインデントされたコードでは' end'sはありません。あなたはコードをインデントした*と*を修正したとしますが、あなたの答えはインデントだけが問題だったように見えます。 @Garethが指摘するように、 'elsif'問題も修正しました。 – Zabba

+0

あなたは正しいです、私はまた、端とelsifの問題を修正しました。それはまったく言及しなかった。ありがとう、答えを編集するつもり! – tbuehlmann

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: 
    ... 

この私は思っているだけでなく、トリックを行う必要があります。

関連する問題