我需要編寫一個程序,將正常的語音轉換爲拉丁語。我寫意想不到的結束肯德寶石明膠程序
def translate(*string)
word = []
word_string = []
s = []
i = 0
a = nil
#enters input from user into individual arrays spots
#removes spaces
string[0].scan(/\w+/).each { |x| word << x }
#goes through each word and does operation
while i < word.count
word[i].scan(/./) { |x| word_string << x }
#checks if starts with vowel
if word_string[0].include?("aeiou" || "AEIOU")
word_string = word_string << "ay"
s[i] << word_string.join('')
#checks if starts with Qu or qu
elsif word_string[0] + word_string[1] == "qu" || word_string[0] + word_string[1] == "Qu"
word_string.delete_at(0) && word_string.delete_at(1)
word_string << "quay"
s[i] = word_string.join('')
#checks if starts with 3 consonants
unless (word_string[0] + word_string[1] + word_string[2]).include?("aeiou")
a = word_string[0] + word_string[1] + word_string[2]
word_string.delete_at(0) && word_string.delete_at(1) && word_string.delete_at(2)
word_string << (a + "ay")
s[i] = word_string.join('')
a = nil
#checks if starts with 2 consonants
unless (word_string[0] + word_string[1]).include?("aeiou")
a = word_string[0] + word_string[1]
word_string.delete_at(0) && word_string.delete_at(1)
word_string << (a + "ay")
s[i] = word_string.join('')
a = nil
#check if starts with 1 consonants
else
a = word_string[0]
word_string.delete_at(0)
word_string << (a + "ay")
s[i] = word_string.join('')
a = nil
end
i += 1
end
s.join(" ")
end
它退還給我一個錯誤說
pig_latin.rb:58: syntax error, unexpected $end, expecting kEND
我看着錯誤,這意味着我要麼錯過了結束的地方還是我有太多了,但我不能找到它。我有一個結束,一段時間結束,如果結束,所以問題不在那裏。我認爲它可能在前幾個鏈接中的某個地方,在那裏我編寫了掃描來初始排序文本,但它看起來並不像它那裏。我需要另一雙眼睛來看看,我找不到它。此外,如果有更好的方式來寫這個,請讓我知道。
'if','elsif'和'else'應該是一個塊。不要混合'除非'和其他。如果要執行多個檢查,則使用'case'語句,這將更易於理解和調試。 – Kashyap 2013-04-22 02:35:45
我最初編寫的除非語句爲 'elsif(word_string [0] + word_string [1])。!include?(「aeiou」)' 但我做了更多的閱讀,看起來好像你可以做的那。是否有另外一種方法來編寫該部分? – 2013-04-22 02:46:37
就像我說的,使用'case'語句,你會更好。既然你問了,'elsif'部分可以寫成:'elsif!((word_string [0] + word_string [1])。include?(「aeiou」))''。 – Kashyap 2013-04-22 03:45:13