Ruby:语法错误,意外的keyword_when,期待输入结束

时间:2017-01-10 17:07:42

标签: ruby

我试图修改脚本以包含一个案例块,但每当我这样做时,我似乎都会收到此错误。我最初认为这是因为我在某个地方错过了一个结束但我检查了我的整个代码,它似乎检查得很好。

工作(在添加案例之前):

 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

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您不需要end块内的when语句。这就是解雇翻译的问题。此外,您的多值when是错误的;它应该是when 9, 10而不是when 9 || 10,因为这会评估为真实价值。

删除它们,您的新代码应与原始代码等效。

答案 1 :(得分: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