问题是您从未将j 重置为零。
第一次运行外部while 循环时,即比较string 的第一个字符与每个元音,j 从 0(对于“a”)递增到 4(对于“u”) .然而,第二次外部循环运行时,j 已经是 4,这意味着它随后会递增到 5、6、7 等等。 vowels[5]、vowels[6] 等都计算为 nil,因此第一个之后的字符永远不会算作元音。
如果您将j = 0 行移到外部while 循环内,则您的方法可以正常工作。
您的第二个问题是关于.each,表明您已经在正确地思考问题。 while 在 Ruby 中很少见,.each 肯定是一个改进。事实证明,您不能在 String 上调用 .each(因为 String 类不包含 Enumerable),因此您必须首先使用 String#chars 方法将其转换为字符数组。这样,您的代码将如下所示:
def count_vowels(string)
chars = string.chars
vowels = ["a", "e", "i", "o", "u"]
count = 0
chars.each do |char|
vowels.each do |vowel|
if char == vowel
count += 1
break
end
end
end
puts count
end
不过,在 Ruby 中,我们有更好的方法来做这种事情。一个特别适合这里的是Array#count。它需要一个块并为数组中的每个项目评估它,然后返回该块返回 true 的项目数。使用它我们可以编写这样的方法:
def count_vowels(string)
chars = string.chars
vowels = ["a", "e", "i", "o", "u"]
count = chars.count do |char|
is_vowel = false
vowels.each do |vowel|
if char == vowel
is_vowel = true
break
end
end
is_vowel
end
puts count
end
不过,这并不短。我们可以使用的另一个好方法是Enumerable#any?。它为数组中的每个项目评估给定块,并在找到该块返回 true 的任何项目时返回 true。使用它使我们的代码超级短,但仍然可读:
def count_vowels(string)
chars = string.chars
vowels = %w[ a e i o u ]
count = chars.count do |char|
vowels.any? {|vowel| char == vowel }
end
puts count
end
(在这里你会看到我引入了另一个常见的 Ruby 习语,用于创建数组的“百分比文字”表示法:%w[ a e i o u ]。这是创建字符串数组的常用方法,无需所有这些引号和逗号. 你可以read more about it here.)
做同样事情的另一种方法是使用Enumerable#include?,如果数组包含给定项,则返回true:
def count_vowels(string)
vowels = %w[ a e i o u ]
puts string.chars.count {|char| vowels.include?(char) }
end
...但事实证明,String 也有一个 include? 方法,所以我们可以这样做:
def count_vowels(string)
puts string.chars.count {|char| "aeiou".include?(char) }
end
还不错!但我把最好的留到了最后。 Ruby 有一个很棒的方法叫做String#count:
def count_vowels(string)
puts string.count("aeiou")
end