Ruby语法错误只说";出乎意料的'\n',期望=>



我迫切需要了解这段代码的问题是什么,和往常一样,当我试图用条件语句编写while循环这样简单的代码时,不知怎么地我收到了一条语法错误消息:语法错误,意外的"\n",应为=>

我知道还有另一个SO问题问到了这一点,但问题是其他代码涉及哈希和我没有使用的东西——我在这里所要做的就是读入一个数字,并根据输入生成一组响应中的一个。但我这辈子从来没有一个程序刚刚启动,甚至拒绝具体说明我需要做什么!

这是我的代码,:

#!/usr/bin/ruby
time=-1
while (time < 0)
print "Enter the total number of minutes you spend per week reading/studying: ",
time => gets.to_i
if (time >= 120)
{
puts => "Congratulations! You will likely pass the course."
}
elsif (time > -1)
{
if (time > 60)
{
puts=>"OK...You may still pass the course."
}
else
{
puts=>"I have grave concerns about your study habits."
}end 
end

我试着运行这个,它只告诉我它期望"=>"以某种方式。如果结束位不完全是"}end end",Ruby就会大发雷霆,告诉我在它期望"keyword_end"的地方有意外的"}",但当我更改它时,它立即告诉我它不期望keyword_end-instances它只是告诉我放入,它期望它告诉我删除的方括号

在while循环中,我会把整件事都用括号括起来,但后来这件事吓坏了,因为它坚持认为第一个"if"语句所在的地方没有结束括号。

受够了,我已经尝试了几个小时,我准备放弃,因为Ruby的错误消息是我遇到过的最无助的事情!

更正代码:

print "Enter the total number of minutes you spend per week reading/studying: "
time = gets.to_i
if time >= 120
puts "Congratulations! You will likely pass the course."
elsif time > 60
puts "OK...You may still pass the course."
else
puts "I have grave concerns about your study habits."
end 

研究一下这是怎么写的,你应该没事。

最新更新