Ruby中的参数错误,因为无法运行prograim

问题描述 投票:0回答:1

我编写了以下代码,该代码从用户那里读取输入,将它们放入一个数组中,然后在终端中打印出该数组,但是每次运行它时,我总是收到以下错误,并且不确定为什么:] >

plane.rb:9:in`initialize':错误的参数数量(给定0,预期为4)(ArgumentError)

require './input_functions'

# Complete the code below
# Use input_functions to read the data from the user

class Plane
    attr_accessor :id, :number, :origin, :destination

    def initialize (id, number, origin, destination)
        @id=id
        @number = number
        @origin = origin
        @destination = destination
    end 
end 



def read_plane()

    plane_id = read_integer("Enter plane id:")
    plane_number = read_string("Enter flight name:")
    plane_origin = read_string("Enter origin airport:")
    plane_destination = read_string("Enter destination airport:")
    plane = Plane.new()
    plane.id = plane_id
    plane.number = plane_number
    plane.origin = plane_origin
    plane.destination = plane_destination

end

def read_planes()
    planes=Array.new()
    count=0
    while (count<4)
        planes<<read_plane()
        count+=1
    end 


end

def print_plane(plane)
    puts ("Plane id "+plane.id)
    puts ("Flight "+plane.number)
    puts ("Origin "+plane.origin)
    puts ("Destination "+plane.destination)

end

def print_planes(planes)
    index = 0
    while (index<planes.length)
        puts planes[index]
        index+=1
    end 

end

def main()
    planes = read_planes()
    print_planes(planes)
end

main()

我编写了以下代码,该代码从用户那里读取输入,将它们放入一个数组中,然后在终端中打印出该数组,但是每次我运行它时,都会不断出现以下错误,并且...]]

ruby
1个回答
0
投票

Plane类在初始化时需要参数idnumberorigindestination(请参见initialize方法)。但是,您试图通过它们的getter(通过attr_accessor创建)传递这些值。

您需要将代码更改为:

© www.soinside.com 2019 - 2024. All rights reserved.