我被困在下面的练习中。
说明:
在您的“添加”时块中,删除“添加的”看跌期权。声明。
在其位置提示用户输入电影标题。将结果保存到名为title的新变量中。 (您的代码已经有一个执行此操作的示例!)
接下来,提示用户输入电影的分级。将其保存到一个新的名为rating的变量中。
将该电影/评级对添加到电影哈希中,并显示一条消息,指示已添加该对。 (现在还不需要to_sym或to_i!)
代码:
movies = {"good fellas"=> "5"}
puts "what's your favorite movie?"
choice=gets.chomp
case choice
when "add"
puts "what's the movie you would like to add?"
title=gets.chomp
movies[title]
puts"what's the rating for your selected movie?"
rating=gets.chomp
movies[title]=rating
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end
答案 0 :(得分:0)
可以将键/值对添加到哈希中,如下所示:
hash[key] = value
所以在您的情况下:
when "add"
puts "what's the movie you would like to add?"
title=gets.chomp
puts "what's the rating for your selected movie?"
rating=gets.chomp
movies[title] = rating
puts "movie added"
答案 1 :(得分:0)
以下应为您的代码
movies = {"good fellas"=> "5"}
puts "what's your favorite movie?"
choice=gets.chomp
case choice
when "add"
puts "what's the movie you would like to add?"
title=gets.chomp
#movies[title]
puts"what's the rating for your selected movie?"
rating=gets.chomp
movies[title]=rating
when "update"
puts "Updated!"
when "display"
puts "Movies!"
when "delete"
puts "Deleted!"
else
puts "Error!"
end