嗨,我是编程和Ruby的新手。
在这个脚本中,我是一个面包师,我想做饼干。面包师即用户将给出烘焙时间作为输入,并且程序需要将该输入时间与恒定的配方时间进行比较。
当食谱时间=烘焙时间时,将通知面包师他们已准备好。例如,用户给出的cooking_time为10,Recipe_time为8.当我们烘焙cookie时,cooking_time - = 1,一旦达到2即到达recipe_time,cookie就会准备就绪并且用户会提示将它们取出?如果没有,cooking_time将继续减少,这将导致饼干被烧毁。
进行这种比较最好的是什么?
以下是我到目前为止所得到的,我现在已经准备好了?准备好了?'方法。谢谢你们!
class Cookies
attr_reader :doughy,:almost_ready,:ready,:burned
attr_accessor :baking_time
def initialize
@perfection = false
end
def ready?
while @perfection != true
#This is where I got Stuck
end
end
def perfection?
@perfection = true if @baking_time = @recipe_time
end
def move_out
answer = ""
puts "It took #{@elapsed_time} minutes but our #{@name} are ready! Do you want to take them out? Answer Y/N"
answer = gets.chomp
if answer == "Y"
puts "Great! Leave them to cool for 2 minutes then we can eat 'em!"
else
puts 'burnt'
end
end
def bake
@start_time = Time.now
@baking_time -= 1
ready?
end
end
class ChocChip < Cookies
attr_reader :recipe_time, :name
def initialize(baking_time)
@name = "Chocolate Chip Cookies"
@baking_time = baking_time
@recipe_time = 8
end
end
class Muesli < Cookies
attr_reader :recipe_time, :name
def initialize(baking_time)
@name = "Muesli Cookies"
@baking_time = baking_time
@recipe_time = 10
end
end
puts "WELCOME TO I <3 COOKIES BAKERY\n\n"
puts "What kind of cookie would you like to bake?\n\n"
puts "Enter 1 for CC or 2 for Muesli"
cookie_choice = gets.chomp
puts "How long would you want to bake them for?"
cookie_baking_time = gets.chomp.to_i
if cookie_choice == "1"
my_choc_chips = ChocChip.new(cookie_baking_time)
my_choc_chips.bake
elsif cookie_choice == "2"
my_muesli = Muesli.new(cookie_baking_time)
my_muesli.bake
else
puts "Err..please choose a type of cookie you want to bake!"
end
答案 0 :(得分:2)
烘焙花费时间,而不是检查准备情况:)
def ready?
@baking_time == @recipe_time
end
def bake
@baking_time = 0
while !ready? do
@baking_time += 1
sleep 1 # sleep for one second
end
end
以上将停止@recipe_time
的执行。无论你是想假冒烘焙,都要评论sleep
。