2017-01-10 94 views
-2

我想修改一个脚本来包含一个case块,但是每当我这样做,我似乎得到这个错误。我最初认为这是因为我错过了某个地方的结局,但我检查了我的整个代码,看起来好像很好。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

**缩进**请!如果错误是可见的,你会发现这样的错误。这一切完全混乱是一个巨大的问题。第二件事是避免使用全局变量,除非它是绝对必要的。 Ruby的'$ x'不像PHP或Perl等价物那样只是一个变量,'$'前缀意味着全局。此外,Ruby风格建议对方法和变量名使用'no_right',所有小写都带下划线,因为在Ruby中情况具有重要意义。 – tadman

回答

0

您不需要when块中的end语句。这就是抛弃解释器的原因。此外,您的多值when是错误的;它应该是when 9, 10而不是when 9 || 10,因为这将评估为真值。

删除这些和您的新代码应该等同于您的原始代码。

+0

我改变了它,它仍然不会运行,改变“when <= 5”到“when $ noRight <= 5”它运行后,但现在最终的输出是错误 - 原代码工作正常。 –

+0

案件陈述并非真正意义上的不平等。你可以做的最好的可能是'当5,4,3,2,1'时代替。 – Makoto

+0

Case语句完全用于ruby中的不等式,使用'-Float :: INFINITY..5'而不是'<= 5'和voilà。 – 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

相关问题