我正在写一个简短的程序,要求用户输入汽车模型,制造商和年份输入,并通过算法传递该输入。我的问题是,是否有方法在通过公式将多个打印输出标记为每个输出的编号后标注多个打印输出?我需要为每个循环使用一个吗?我只是想了解我将如何完成这一任务。例如Ruby标签打印输出
例如打印输出看起来像这样。
class Car
attr_reader :make, :model, :year
def initialize
end
def set_make(make)
@make = make
end
def set_model(model)
@model = model
end
def set_year(year)
@year = year
end
def get_make
@make
end
def get_year
@year
end
def get_model
@model
end
end
array_of_cars = Array.new
print "How many cars do you want to create? "
num_cars = gets.to_i
for i in 1..num_cars
puts
print "Enter make for car #{i}: "
make = gets.chomp
print "Enter model for car #{i}: "
model = gets.chomp
print "Enter year of car #{i}: "
year = gets.to_i
c = Car.new
c.set_make(make)
c.set_model(model)
c.set_year(year)
array_of_cars << c
end
puts
puts "You have the following cars: "
puts
for car in array_of_cars
puts "#{car.get_year} #{car.get_make} #{car.get_model}"
end
puts
- 2014福特Expedition
- 2017年丰田86
- 2017年阿斯顿·马丁DB11
是有办法的号码添加到输出?
谢谢你向我展示如何得到它! –
不用客气,也可以参考1..num_cars部分的''我也可以看看[Ruby#Enumerator](https://ruby-doc.org/core-2.2.0/ Enumerator.html)。 –