我必须更新并将电影标题及其相关评级添加到现有哈希。这是我到目前为止的代码:
movies = {
Titanic:4,
Bestman: 3,
Agora: 2
}
puts "What would you like to do?"
choice = gets.chomp
case movies
when "add"
puts "What movie do you want to add?"
title = gets.chomp
puts "What's the rating of the movie?"
rating = gets.chomp
movies[title] = rating
puts "#{title} has been added with a rating of #{rating}."
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end
当我运行代码时,我收到此错误“看起来你没有添加到电影哈希”
我知道错误介于这两行之间:
case movies
when "add"
puts "What movie do you want to add?"
title = gets.chomp
puts "What's the rating of the movie?"
rating = gets.chomp
movies[title] = rating
puts "#{title} has been added with a rating of #{rating}."
我一直想弄清楚,但到目前为止还没有弄清楚我做错了什么。
有没有其他方法可以添加到我的电影哈希?我的put代码有什么问题让用户知道他/她的电影标题和评级已被添加?
谢谢
修改 正如Some Guy所指出的,改变案例陈述来自
case movies
到
case choice
解决了这个问题。
我需要学习/弄清楚为什么第二部作品,但第一部作品没有。
答案 0 :(得分:1)
更改
case movies
到
case choice # because this is where your choice of what to do is being stored
答案 1 :(得分:0)
如果仍然不清楚,可能有助于将案例陈述视为一系列if / elsif(存在差异,但对于这种情况,它有效),所以:
case movies
when "add" # do stuff
类似于:
if movies == "add"
# do stuff
end
希望能让它更清晰。