【发布时间】:2019-06-07 03:12:58
【问题描述】:
我只是想让这段代码正常工作,而且我不断得到:
请告诉我我做错了什么。
我正在学习,我已经完成了我能想到的代码的每一次迭代,试图让它工作,我只是不确定我做错了什么。
puts "How old are you?"
old = gets.chomp
if old <= 21
return "You are not legally allowed to buy alcohol in the US"
else
return "You are legally allowed to buy alcohol in the US"
end
【问题讨论】:
-
如果
old.to_i <= 21。gets.chomp.to_i与get.to_i相同。 -
“我正在尝试将一个值转换为整数” – 您似乎意识到您必须将输入值转换为整数。但是您的代码中不会发生转换。您正在将未转换的输入与整数 21 进行比较,而 Ruby 告诉您它无法以这种方式将字符串与
21进行比较。