2

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)
4

2 に答える 2

1

メソッドを呼び出すメソッドを使用して#{target_temp}います。 @ 記号をその前に置くか、それらのアクセサメソッドを追加できます。私は後者の方が好きです。

class House
  attr_accessor :target_temp, :min_temp, :max_temp
于 2013-05-26T09:38:55.207 に答える
-1

以下のように、@記号を見逃しました。#{target_temp}#{min_temp}

  • puts "The current temperature is #{degrees_ac_off} below the target temperature of #{target_temp}. The air conditioner is off."

  • puts "The current temperature is #{degrees_below} the minimum desired temperature of #{min_temp}. The heater is on."

  • puts "The current temperature is #{degrees_high} more than the target temperature of #{target_temp}. The air conditioner is on low."

  • puts "The current temperature is #{degrees_ac} more than the target temperature of #{target_temp}. The air conditioner is on high."

以下のコードを使用します。

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_high = @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)
#=> The current temperature is 9 more than the target temperature of 71. The air conditioner is on high.
于 2013-05-26T09:35:31.827 に答える