gpt4 book ai didi

ruby - 智能家居恒温器

转载 作者:太空宇宙 更新时间:2023-11-03 16:52:58 24 4
gpt4 key购买 nike

Ruby 新手,正在做我自己版本的智能恒温器练习。我想设置一个已初始化的目标温度参数,然后在方法 update_temperature 中使用当前温度参数!出于某种原因,它会抛出有关未定义的局部变量的错误。

有什么想法吗?

class House
def initialize(target_temp, max_temp, min_temp)
@target_temp = target_temp
@max_temp = max_temp
@min_temp = min_temp
end

def update_temperature!(current_temp)
@current_temp = current_temp
if @min_temp < @current_temp && @current_temp < @target_temp
degrees_ac_off = @target_temp - @current_temp
puts "The current temperature is #{degrees_ac_off} below the target temperature of #{target_temp}. The air conditioner is off."
elsif @current_temp < @min_temp
degrees_below = @min_temp - @current_temp
puts "The current temperature is #{degrees_below} the minimum desired temperature of #{min_temp}. The heater is on."
elsif @target_temp < @current_temp && @current_temp < @max_temp
degrees_ac = @current_temp - @target_temp
puts "The current temperature is #{degrees_high} more than the target temperature of #{target_temp}. The air conditioner is on low."
else @max_temp < @current_temp
degrees_ac = @current_temp - @target_temp
puts "The current temperature is #{degrees_ac} more than the target temperature of #{target_temp}. The air conditioner is on high."
end
end
end


my_house = House.new(71,75,60)
my_house.update_temperature!(80)

最佳答案

您正在使用调用方法的#{target_temp},您可以将@符号放在它前面或为它们添加访问器方法。我更喜欢后者。

class House
attr_accessor :target_temp, :min_temp, :max_temp

关于ruby - 智能家居恒温器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16757925/

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