gpt4 book ai didi

ruby - 整数值无效

转载 作者:数据小太阳 更新时间:2023-10-29 09:00:54 24 4
gpt4 key购买 nike

这段代码:

my_name = 'Alessandro Tegagni'
my_age = '31'
my_height = '170 '#cm
my_weight = '82 '#kg
my_eyes = 'Brown'
my_teeth = 'White'
my_hair = 'Brown'

puts "let is talk about %s." % my_name
puts "he is %d cm tall." % my_height
puts "he is %d kg heavy." % my_weight
puts "actually that is not too heavy."
puts "he is got %s eyes and %s hair." % [my_eyes,my_hair]
puts "his teeth are usually %s depending on the coffee or tea" % my_teeth

puts "if I add %d, %d, and %d I get %d." % [my_age,my_height,my_weight,my_age+my_height+my_weight]

引发错误:

:17:in `%': invalid value for Integer(): "31170 82 " (ArgumentError)

错误是什么?

最佳答案

问题是您将变量定义为字符串。在字符串上使用 + 将它们连接起来,这就是您在 31170 82 中看到的。要解决这个问题,请将整数值分配给变量,而不是字符串:

my_name = 'Alessandro Tegagni'
my_age = 31
my_height = 170 #cm
my_weight = 82 #kg
my_eyes = 'Brown'
my_teeth = 'White'
my_hair = 'Brown'

这应该足以使您的代码正常工作。

关于ruby - 整数值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com