看看這段代碼。我得到了期望的結果,即掃描一個人的輸入以查看它是否與內部數組匹配。如何阻止重複的行?
sentence = []
compare = []
database_array = ["Mouse", "killer", "Blood", "Vampires", "True Blood", "Immortal" ]
def parser_sentence compare
database_array = ["Mouse", "killer", "Blood", "Vampires", "True Blood", "Immortal"]
initial_index = 0
while compare.count > initial_index
compare.each do |item|
if item == database_array[initial_index]
puts "You found the key word, it was #{item}"
else
puts "Sorry the key word was not inside your sentence"
end
end
initial_index = initial_index + 1
end
end
puts "Please enter in your sentences of words and i will parse it for the key word."
sentence = gets.chomp
compare = sentence.split (" ")
因爲每個循環都告訴它重複,它是這樣做的,但是我怎麼能阻止它呢?
請根據輸入添加輸入和輸出。 –
在這裏重複'database_array'是否有任何理由? – tadman
@tadman這可能是錯誤的複製粘貼,我想.. :) –