尝试使用Ruby while循环查找字符串的元音

wan*_*mer 3 ruby arrays while-loop

def count_vowels(string)
  vowels = ["a", "e", "i", "o", "u"]
  i = 0
  j = 0
  count = 0

  while i < string.length do
    while j < vowels.length do
      if string[i] == vowels[j]
        count += 1
        break
      end

      j += 1
    end

    i += 1
  end

  puts count 
end
Run Code Online (Sandbox Code Playgroud)

我无法发现出错的地方.如果该程序遇到辅音,它就会停止.另外,使用".each"方法如何解决同样的问题?

Jor*_*ing 11

问题是你永远不会重置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肯定会有所改进.事实证明,您无法调用.eachString(因为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
Run Code Online (Sandbox Code Playgroud)

但是在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
Run Code Online (Sandbox Code Playgroud)

但是,这并不短.我们可以使用的另一个好方法是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
Run Code Online (Sandbox Code Playgroud)

(在这里你会看到我投入另一个常见的Ruby习语,用于创建数组的"百分比文字"符号:%w[ a e i o u ].这是创建一个没有所有引号和逗号的字符串数组的常用方法.你可以阅读更多关于它在这里.)

另一种做同样事情的方法是使用Enumerable#include?,如果数组包含给定项,则返回true:

def count_vowels(string)
  vowels = %w[ a e i o u ]  
  puts string.chars.count {|char| vowels.include?(char) }
end
Run Code Online (Sandbox Code Playgroud)

...但事实证明,String也有一个include?方法,所以我们可以这样做:

def count_vowels(string)
  puts string.chars.count {|char| "aeiou".include?(char) }
end
Run Code Online (Sandbox Code Playgroud)

不错!但是我最后一次救了最好的.Ruby有一个很好的方法叫做String#count:

def count_vowels(string)
  puts string.count("aeiou")
end
Run Code Online (Sandbox Code Playgroud)