gpt4 book ai didi

ruby - 这个计算元音的 Ruby 程序有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-03 17:10:23 27 4
gpt4 key购买 nike

当我在字符串上测试这个程序时,输出为 0。我认为我的逻辑是合理的,它只是一个次要的语法问题。有人看到问题了吗?

def VowelCount(string)

string.downcase
i = 0
vowels = 0

until i == string.length-1
if (string[i] == "a" || string[i] == "o" || string[i] == "e" || string[i] == "i" || string[i] == "u")
vowels += 1
end
i += 1
end
return vowels
end

最佳答案

您可以使用 String#count :

str = "It was the best of times, it was the worst of times,..."

str.downcase.count('aeiou') #=> 14

关于ruby - 这个计算元音的 Ruby 程序有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26953202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com