在Ruby中如何将表达式和整数进行比较?
我正在学习Ruby,但遇到了一些问题。我尝试将表达式的和与整数进行比较,得到了以下结果:“将字符串与2000进行比较失败”。非常感谢在Ruby中如何将表达式和整数进行比较?,ruby,Ruby,我正在学习Ruby,但遇到了一些问题。我尝试将表达式的和与整数进行比较,得到了以下结果:“将字符串与2000进行比较失败”。非常感谢 puts "Hello! Please type here your birthday date." puts "Day" day = gets.chomp day.capitalize! puts "Month" month = gets.chomp month.capitalize! puts "Year" year = gets.chomp year.
puts "Hello! Please type here your birthday date."
puts "Day"
day = gets.chomp
day.capitalize!
puts "Month"
month = gets.chomp
month.capitalize!
puts "Year"
year = gets.chomp
year.capitalize!
if month + day + year > 2000
puts "Sum of all the numbers from your birthday date is more than 2000"
else month + day + year < 2000
puts "Sum of all the numbers from your birthday date is less than 2000"
end
显示“你好!请在这里键入您的生日。”
“天”
day=get.chomp
天啊,资本化!
“月”
month=get.chomp
月份,大写!
“年”
year=get.chomp
年,资本化!
如果月+日+年>2000
将“您生日日期的所有数字之和超过2000”
其他月份+天+年<2000年
放入“您生日日期的所有数字之和小于2000”
结束
这里的day
是一个字符串。而month+day+year
也是一个字符串,只是更长。要获取整数,请调用。\u i
day = gets.to_i # to_i will handle the newline, no need to chomp.
# repeat for month and year
(当然,一旦您将字符串转换为整数,您将无法将其大写。无论如何,这毫无意义。)将整数大写?为什么?@YevgeniyAnfilofyev:他们变大了:)当然,再投资;)674.capitalize=>dclxxvit有效!非常感谢。看来我还没有读到这一点。
day = gets.to_i # to_i will handle the newline, no need to chomp.
# repeat for month and year