我需要在用戶輸入的字符串的每一行中計算元音,單詞,代詞(「他,她,他們」)的數量。如果輸入是「他們在玩,他在學習」輸出的預期是句子1有3個單詞,有4個元音,1個代詞。 \ n句子2有3個單詞,4個元音,1個代詞。我寫了下面的代碼,但得到一個錯誤意外結束輸入。計算每個字符串中元音,代詞的數量?
string = gets
string =string.chomp
sentencecount = 0
wordcount = 0
pronouns={"He"=>0,"She"=>0,"They"=>0,"Them"=>0}
procount=0;
string.split(".").each do |sentence|
wordcount = 0
sentencecount += 1 #tracking number of sentences
vowels=sentence.scan(/[aeoui]/).count
procount=0
sentence.split(/\w+/).each do |word|
pronouns.each do|key,value|
if (key.eq word)
procount++
wordcount += 1 # tracking number of words
end
puts "Sentence #{sentencecount} has #{wordcount} words, has #{vowels} vowels"
end
它每次運行時給出0作爲代詞 –
是的,在你的代碼中你不更新它們。 – Ursus
給出了procount + = 1之後它也沒有更新..可以請你告訴我如何更新 –