0
我創建了一個包含奧斯卡信息的對象數組,使用帶有所有類別名稱,獲獎者和提名者的文本文件(獲獎者出現在提名名單中作爲好)。我現在想要問一個用戶。你想知道哪個類別的獲勝者?一旦問題被問到,它會返回答案。我只能讓它在數組的最後一個對象上工作(最好的視覺效果會返回重力)。有人可以解釋爲什麼會這樣嗎?根據Ruby中的對象數組輸入正確的對象屬性
class AwardCategory
attr_accessor :winner, :name, :nominees
def initialize(name)
@name = name
@nominees = []
end
end
class Nominee
attr_accessor :name
def initialize(name)
@name = name
end
end
file = File.open('oscar_noms.txt', 'r')
oscars = []
begin
while true do
award_category = AwardCategory.new(file.readline.downcase)
award_category.winner = file.readline.downcase
nominee = Nominee.new(file.readline.downcase)
award_category.nominees << nominee
next_nominee = Nominee.new(file.readline.downcase)
until next_nominee.name == "\n"
award_category.nominees << next_nominee
next_nominee = Nominee.new(file.readline.downcase)
end
oscars << award_category
end
rescue EOFError => e
puts 'rescued'
end
#puts oscars.inspect
#Read input here
puts "What category do you want to know the winner for?"
answer = gets
oscars.each
if answer.downcase == award_category.name
puts award_category.winner
else
puts "That is not a category"
end
我很感激幫助!該陣列被稱爲奧斯卡獎,因爲它擁有提名者,獲勝者和類別。我想最終設定它來回答有關奧斯卡的任何問題。 –