Ruby 获取用户输入x次并输出所有狗数据集
我正在创建一个程序,为一只狗创建一个记录,该记录具有属性(id、出生年份和名称)。我必须使用数组来存储3条狗的属性,然后将它们打印到终端Ruby 获取用户输入x次并输出所有狗数据集,ruby,loops,Ruby,Loops,我正在创建一个程序,为一只狗创建一个记录,该记录具有属性(id、出生年份和名称)。我必须使用数组来存储3条狗的属性,然后将它们打印到终端 require './input_functions' class Dog attr_accessor :id, :breed, :year_born, :name def initialise (id, breed, year_born, name) @id = id @breed = breed @year_born =
require './input_functions'
class Dog
attr_accessor :id, :breed, :year_born, :name
def initialise (id, breed, year_born, name)
@id = id
@breed = breed
@year_born = year_born
@name = name
end
end
# Complete the missing code below
# Note: If your tasks requires outputting a floating point number you may wish to use the following:
# print out my_variable with 2 decimal places:
# printf("$%.2f\n", my_variable)
def read_dog()
puts "Enter the ID of your dog"
$dog_id = gets.to_s
puts "Enter the Breed of your dog"
$dog_breed = gets.to_s
puts "Enter the birth year of your dog"
$dog_year_born = gets.to_s
puts "Enter the name of your dog"
$dog_name = gets.to_s
end
def display_dog
puts $dog_id + "The dog ID is an Integer which is unique to your dog"
puts $dog_breed + "The dog breed is a String which defines the ancestors of your dog"
puts $dog_year_born + "The year born is an Integer which contains what year your dog was born"
puts $dog_name + "The dog name is a String which contains what your dog's name is"
end
def read_dogs
dogs = Array.new
index = 0
while (index < 3)
dogs << read_dog[index]
index = index + 1
end
return dogs
end
def display_dogs(dogs)
index = 0
while (index < 3)
dogs[display_dog.to_i]
index = index + 1
end
end
def main
dogs = read_dogs
display_dogs(dogs)
end
main
require./input\u函数'
班犬
属性访问者:id,:品种,:出生年份,:姓名
def初始化(id、品种、出生年份、姓名)
@id=id
@品种
@出生年份=出生年份
@name=name
结束
结束
#完成下面缺少的代码
#注意:如果您的任务需要输出浮点数,您可能希望使用以下方法:
#打印带2位小数的my_变量:
#printf(“$%.2f\n”,我的变量)
def read_dog()
输入“输入您的狗的ID”
$dog\u id=get.to\u s
放入“输入您的狗的品种”
$dog\u breed=gets.to\u
输入“输入您的狗的出生年份”
$dog\u year\u born=到达
输入“输入您的狗的名字”
$dog\u name=get.to\u s
结束
显示狗
放置$dog_id+“dog id是一个整数,对您的狗来说是唯一的”
放置$dog_BRIDE+“dog BRIDE是定义您的狗的祖先的字符串”
放置$dog\u year\u born+“出生年份是一个整数,包含您的狗出生的年份”
放置$dog_name+“dog name是一个字符串,其中包含您的狗的名称”
结束
def read_狗
dogs=Array.new
索引=0
而(指数<3)
dogs首先,您必须从read\u dog方法返回一些值
def read_dog()
puts "Enter the ID of your dog"
$dog_id = gets.to_s
puts "Enter the Breed of your dog"
$dog_breed = gets.to_s
puts "Enter the birth year of your dog"
$dog_year_born = gets.to_s
puts "Enter the name of your dog"
$dog_name = gets.to_s
return [$dog_id, $dog_breed, $dog_year_born, $dog_name]
end
让我告诉你其他的事情,因为它只打印最后的信息
顺便说一句,我目前在您的代码片段中没有看到任何使用Dog类代码的情况首先,您必须从read\u Dog方法返回一些值
def read_dog()
puts "Enter the ID of your dog"
$dog_id = gets.to_s
puts "Enter the Breed of your dog"
$dog_breed = gets.to_s
puts "Enter the birth year of your dog"
$dog_year_born = gets.to_s
puts "Enter the name of your dog"
$dog_name = gets.to_s
return [$dog_id, $dog_breed, $dog_year_born, $dog_name]
end
class Dog
attr_accessor :id, :breed, :year_born, :name
def self.ask_for_dogs(n_times)
all_dogs = []
n_times.times do
all_dogs << new
end
all_dogs
end
def self.display(dogs)
dogs.each do |dog|
puts "Id: #{dog.id}"
puts "Name: #{dog.name}"
puts "Breed: #{dog.breed}"
puts "Year born: #{dog.year_born}\n"
end
end
private
def initialize
puts "Enter the dog's ID:"
@id = gets.to_s
puts "Enter the dog's breed:"
@breed = gets.to_s
puts "Enter the dog's birth year:"
@year_born = gets.to_s
puts "Enter the dog's name:"
@name = gets.to_s
end
end
让我告诉你其他的事情,因为它只打印最后的信息
顺便说一句,我目前在您的代码片段中没有看到任何使用Dog类代码的情况不确定这是否解决了您的问题,但是您错误地拼写了initialize
@SagarPandya:Dog
类从未在代码中的任何地方使用过,因此它不可能与问题有任何关系。事实上,我不知道OP为什么会在问题中包含它,因为它从来没有被使用过。我不明白这个display\u dog.to\u I东西,我也不明白你为什么不使用dog.new和global($)变量,请看我下面的答案和一个工作示例。可能来自PHPNot,如果这解决了你的问题,但是您错误地拼写了initialize
@SagarPandya:Dog
类从来没有在代码中的任何地方使用过,因此它不可能与问题有任何关系。事实上,我不知道为什么OP甚至把它包括在问题中,因为它从来没有被使用过。我不明白这个display_dog.to_I的东西,我也不明白为什么你不使用dog.new和global($)变量,请看下面我的答案和一个工作示例。可能来自PHP。你为什么使用global variables?你为什么使用global variables?
class Dog
attr_accessor :id, :breed, :year_born, :name
def self.ask_for_dogs(n_times)
all_dogs = []
n_times.times do
all_dogs << new
end
all_dogs
end
def self.display(dogs)
dogs.each do |dog|
puts "Id: #{dog.id}"
puts "Name: #{dog.name}"
puts "Breed: #{dog.breed}"
puts "Year born: #{dog.year_born}\n"
end
end
private
def initialize
puts "Enter the dog's ID:"
@id = gets.to_s
puts "Enter the dog's breed:"
@breed = gets.to_s
puts "Enter the dog's birth year:"
@year_born = gets.to_s
puts "Enter the dog's name:"
@name = gets.to_s
end
end