2017-01-10 19 views
-2

スクリプトを修正してケースブロックを挿入しようとしていますが、いつでもこのエラーが発生するようです。私は当初どこかで終わりを逃していたからだと思っていたが、コード全体をチェックしたところ、ちょっとチェックしたようだ。Ruby:構文エラー、予期せぬkeyword_when、入力の終了を予期します。

ワーキング(ケースを追加する前):後

def determine_grade 
Console_Screen.cls #Clear the display area 
#To pass the test the player must correctly retype 3 sentences 
if $noRight >= 6 then 
#Inform the player of the good news 
print "You retyped " + $noRight.to_s + " sentence(s)" + 
"correctly. " 
puts "You have passed the typing test!\n\nPress Enter" + 
"to continue." 
else #The player has failed the test 
#Inform the player of the bad news 
print "You retyped " + $noRight.to_s + " sentence(s)" + 
"correctly. " 
puts "You have failed the typing test!\n\nPress Enter 
to continue." 
end 
end 
end 

def determine_grade 
Console_Screen.cls #Clear the display area 
#To pass the test the player must correctly retype 6 sentences 
case $noRight 
when 9 || 10 
    print "You get an A!" 
    end 
when 8 
    print "You get a B!" 
    end 
when 7 
    print "You get a C." 
    end 
when 6 
    print "You get a D." 
    end 
when <= 5 
    print "You get an F." 
    end 
else 
    print "Error" 
    end 
end 
end 
end 

任意のアイデア?

+1

**インデント**してください!エラーが見える場合は、このような誤りをキャッチします。これを完全に解体することは大きな問題です。 2つ目は、絶対に必要でない限り、グローバル変数の使用を避けることです。 Rubyの '$ x'は単純に変数であるPHPやPerlのようなものではなく、' $ '接頭辞はグローバルを意味します。さらに、Rubyでは大文字と小文字が区別されるため、方法や変数の名前には 'no_right'(小文字はすべてアンダースコア)を使用することをお勧めします。 – tadman

答えて

0

whenブロック内にendステートメントは必要ありません。それが通訳の拒否です。また、マルチ値whenは間違っています。それはwhen 9 || 10の代わりにwhen 9, 10でなければなりません。それは真実の値に評価されるからです。

これらを削除すると、新しいコードは元のコードと同じになります。

+0

私はそれを変更したが、 "<= 5"のときは "$ noRight <= 5"に変更した後も実行されませんでしたが、最終出力はエラーです - 元のコードはうまくいきました。 –

+0

ケースステートメントは、不等式のためのものではありません。あなたができる最良のものは、代わりに 'when 5,4,3,2,1'でしょう。 – Makoto

+0

Case文は、完全にルビの不等式を意味します。 '<= 5'とvoilの代わりに' -Float :: INFINITY..5'を使います。 – mudasobwa

0
def determine_grade 
    Console_Screen.cls #Clear the display area 
    #To pass the test the player must correctly retype 6 sentences 
    print case $noRight 
     when 9..10 then "You get an A!" 
     when 8 then "You get a B!" 
     when 7 then "You get a C." 
     when 6 then "You get a D." 
     when -Float::INFINITY..5 then "You get an F." 
     else "Error" 
     end 
end 
+0

これを試してみると、結果は表示されません。 –

+0

不可能です。他のどこかで誘発されたエラーがあります。このコードは完全に機能します。 – mudasobwa

関連する問題