当在case语句中输入两个块时,我会收到两次此消息。找不到问题。
choice = gets.chomp.downcase!
case = choice
when "update"
puts "Enter the title of the movie to be updated."
title = gets.chomp.to_sym
if movies[title] == nil
puts "This movie is not in the system."
else
puts "Input the new rating."
rating = gets.chomp.to_i
movies[title] = rating
retry if (rating < 0 || rating > 5)
puts "This rating is invalid! Try again."
end
end
when "display"
movies.each do |title, rating|
puts "#{title}: #{rating} / 5 stars"
end
end代码块进一步扩展到"when“的进一步实例,但为了简单起见,我决定截断它。如有必要,我可以提交完整的代码。这些错误具体如下:
(ruby):57: syntax error, unexpected keyword_when, expecting keyword_end
when "update"
^
(ruby):72: syntax error, unexpected keyword_when, expecting $end
when "display"
^发布于 2014-05-18 16:57:46
您所看到的错误是因为您的语法错误。在缩进代码时要非常小心,这样可以更容易地找到这类bug:
choice = gets.chomp.downcase!
case choice
when "update"
puts "Enter the title of the movie to be updated."
title = gets.chomp.to_sym
if movies[title] == nil
puts "This movie is not in the system."
else
puts "Input the new rating."
rating = gets.chomp.to_i
movies[title] = rating
# retry if (rating < 0 || rating > 5)
puts "This rating is invalid! Try again."
end
when "display"
movies.each do |title, rating|
puts "#{title}: #{rating} / 5 stars"
end
end在end之前有一个额外的when "display"。我想这是为了关闭if (rating < 0 || rating > 5),但是由于它前面有一个语句,所以使它成为一个一行if语句。
即使它是固定的,retry也被滥用了。由于不清楚您到底想做什么,所以我将其注释掉--我建议您阅读相关的文档,重新分级适当的retry语法,并相应地重新编写代码。
发布于 2014-05-18 16:58:03
我想这行case = choice应该是case choice
choice = gets.chomp.downcase!
case choice
when "update"
puts "Enter the title of the movie to be updated."
title = gets.chomp.to_sym
if movies[title] == nil
puts "This movie is not in the system."
else
puts "Input the new rating."
rating = gets.chomp.to_i
movies[title] = rating
if (rating < 0 || rating > 5)
puts "This rating is invalid! Try again."
end
when "display"
movies.each do |title, rating|
puts "#{title}: #{rating} / 5 stars"
end
endhttps://stackoverflow.com/questions/23724445
复制相似问题