小编wan*_*mer的帖子

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

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"方法如何解决同样的问题?

ruby arrays while-loop

3
推荐指数
1
解决办法
3235
查看次数

标签 统计

arrays ×1

ruby ×1

while-loop ×1