打印不在Ruby中工作

时间:2017-07-17 16:11:32

标签: ruby

我为类分配创建了一个非常简单的运行计算器,但我不能让它在一行上显示。我希望输出为“你的锻炼时间是X分钟”。我可以在一行中获得前两个部分,但是当我将“puts workout_time”替换为“print workout_time”时,它不起作用。我得到了一个看起来像进度条的东西,以及“联系服务器”的消息。如果重要,我正在使用repl.it。这是我的代码:

#warm up and cool down times are fixed
#user enters remaining values in minutes

warm_up = 4
cool_down = 4
workout_time = []

puts "How many minutes per set do you want to run?" 
run = gets.chomp.to_i

puts "How many minutes do you want to rest each set?"
rest = gets.chomp.to_i 

puts "How many times do you want to repeat the run/walk?"
sets = gets.chomp.to_i

workout_time << (run+rest)*sets + warm_up + cool_down

print "Your workout time will be "  
puts workout_time
puts " minutes"

谢谢!

2 个答案:

答案 0 :(得分:2)

尝试:

print "Your workout time will be %d minutes" % [workout_time]

答案 1 :(得分:1)

见这里:Concatenating string with number in ruby

您需要使用带有“您的锻炼时间为X分钟”的puts语句,但是将#替换为#{workout_time},如下所示:

puts "Your workout time will be #{workout_time} minutes"