尝试使用Ruby while循环字符串的元音找到 [英] Trying to find vowels of a string using Ruby while loops

查看:126
本文介绍了尝试使用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

我无法察觉这哪里出了问题。如果该程序遇到一个辅音,它停止。此外,如何将同样的问题被使用。每个方法解决呢?

I'm having trouble spotting where this goes wrong. If this program encounters a consonant, it stops. Also, how would the same problem be solved using the ".each" method?

推荐答案

的问题是,你永远不重置Ĵ为零。

The problem is that you never reset j to zero.

第一次的外,而循环运行,这是字符串的第一个字符比较每个元音,Ĵ从0(对于A),以4递增(为U)。外部循环运行所述第二时间,但是,Ĵ已经4,这意味着然后它获取递增到5,6,7和和。 元音[5] 元音[6] 等,都执行为,那么后面的字符的第一个永远不会算作元音。

The first time your outer while loop runs, which is to compare the first character of string to each vowel, j is incremented from 0 (for "a") to 4 (for "u"). The second time the outer loop runs, however, j is already 4, which means it then gets incremented to 5, 6, 7 and on and on. vowels[5], vowels[6], etc. all evaluate to nil, so characters after the first are never counted as vowels.

如果您移动 J = 0 行外,而循环里面,你的方法正常工作。

If you move the j = 0 line inside the outer while loop, your method works correctly.

您的第二个问题,关于。每个,说明你已经沿着正确的方向思考。 ,而很少看到Ruby和。每个肯定是一种进步。事实证明,你不能叫。每个在一个字符串(因为String类不包括的Enumerable ),所以你必须用的 字符串#字符 方法。就这样,你的code是这样的:

Your second question, about .each, shows that you're already thinking along the right lines. while is rarely seen in Ruby and .each would definitely be an improvement. As it turns out, you can't call .each on a String (because the String class doesn't include Enumerable), so you have to turn it into an Array of characters first with the String#chars method. With that, your code would look like this:

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中,虽然,我们有更好的方法可以做到这样的事情。一个适合特别好这里是 阵列#计数 。它需要一个块,并评估其对阵列中的每个项目,然后返回,该块返回真实的项目数。使用它我们可以写这样的方法:

In Ruby, though, we have much better ways to do this sort of thing. One that fits particularly well here is Array#count. It takes a block and evaluates it for each item in the array, then returns the number of items for which the block returned true. Using it we could write a method like this:

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

这不是要短得多,虽然。我们可以用另一个伟大的方法是<一个href=\"http://ruby-doc.org/core-2.1.4/Enumerable.html#method-i-any-3F\"><$c$c>Enumerable#any?.它的计算结果为阵列中的每个项目的给定的块,并返回在找到,该块返回true任何项目真。使用它使我们的code超短期,但仍可阅读:

That's not much shorter, though. Another great method we can use is Enumerable#any?. It evaluates the given block for each item in the array and returns true upon finding any item for which the block returns true. Using it makes our code super short, but still readable:

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 [AEIOU] 这是一个常见方法来创建一个字符串数组没有所有这些引号和逗号。你可以的了解更多关于它这里。)

(Here you'll see I threw in another common Ruby idiom, the "percent literal" notation for creating an array: %w[ a e i o u ]. It's a common way to create an array of strings without all of those quotation marks and commas. You can read more about it here.)

另一种方式做同样的事情将是使用<一个href=\"http://ruby-doc.org/core-2.1.4/Enumerable.html#method-i-include-3F\"><$c$c>Enumerable#include?,如果数组包含给定的项目返回true:

Another way to do the same thing would be to use Enumerable#include?, which returns true if the array contains the given item:

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

...但事实证明,字符串有一个包含方法也一样,所以我们可以做到这一点,而不是:

...but as it turns out, String has an include? method, too, so we can do this instead:

def count_vowels(string)
  puts string.chars.count {|char| "aeiou".include?(char) }
end

不坏!但我已经保存最好的最后一次。 Ruby有一个称为伟大的方法<一href=\"http://www.ruby-doc.org/core-2.1.4/String.html#method-i-count\"><$c$c>String#count:

def count_vowels(string)
  puts string.count("aeiou")
end

这篇关于尝试使用Ruby while循环字符串的元音找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆