gpt4 book ai didi

ruby - Ruby 类中的未定义方法错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:26 25 4
gpt4 key购买 nike

<分区>

我正在尝试为橙树编写一个简单的类...但我收到此错误:pine_orangetree.rb:11:in oneYearPasses': undefined method+' for nil :NilClass (NoMethodError)

当我调用 oneYearPasses 方法时...这是为什么?

class OrangeTree

def initalize
@height = 0
@oranges = 0
end



def oneYearPasses
@height += 2
@oranges = 0

if @height > 7
@oranges += 10
end

if @height < 30
puts "Another year has passed..."
else
puts "The orange tree died..."
end
end

def height
puts "The tree is #{@height} inches tall."
end

def countTheOranges
puts "There are #{@oranges} on the tree."
end

def pickAnOrange
@oranges -= 1
puts "You picked an orange."
puts "There are #{@oranges} left."
end


end

tree1 = OrangeTree.new
tree1.oneYearPasses
tree1.height

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