使用for循环方法打印参数字符串中的元音数量 [英] Using a for loop method to print the number of vowels in the parameter string

查看:57
本文介绍了使用for循环方法打印参数字符串中的元音数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码的输出为:

The output for below code is:

Number of vowels: 2
Number of vowels: 3
Number of vowels: 2
Number of vowels: 0
Number of vowels: 6




但是,如果我将'> ='改为'>'换行:



However, if I change the '>=' to '>' for line:

if (VOWELS.indexOf(word.charAt(i)) >= 0)





输出将变为:



the output will become:

Number of vowels: 2
Number of vowels: 1
Number of vowels: 1
Number of vowels: 0
Number of vowels: 6





我相信使用'>'是正确的方法,但实际的输出并没有产生正确的答案。我的问题是为什么'> ='产生正确答案而不是'>'?









I believe using '>' is the correct way, but the actual output does not produce the right answer. My question is why '>=' produce the correct answer but not '>'?



public class MyProgram
{
    public void start()
    {
        printNumVowels("rumpus");
        printNumVowels("Squabash");
        printNumVowels("Flippant");
        printNumVowels("hymn");
        printNumVowels("MISDEMEANOUR");
    }

    private void printNumVowels(String word)
    {
        final String VOWELS = "aeiouAEIOU";
        int numVowels = 0;
        int lenWord = word.length();

        for (int i = numVowels; i < lenWord; i++)
        {
            if (VOWELS.indexOf(word.charAt(i)) >= 0)
            {
                numVowels++;
            }
        }

        System.out.println("Number of vowels: " + numVowels);
    }
}

推荐答案

当我们计算东西时,我们从一个开始,对吧?这不是计算机程序所做的,它们从零开始。所以索引从0开始,我们凡人总是想到1.

要解释你提到的那行代码,我们从内部开始然后向外移动,如下所示:

1.(word.charAt(i))从单词字符串中获取索引位置i(字符从零开始,记住)的字符。让我们说它是'e'。现在向外移动一步。

2. VOWELS.indexOf(e)表示在VOWELS字符串中找到e并返回其索引位置(同样,它从零开始),如果未找到,它返回-1。

3.这解释了为什么使用> = 0是正确的,因为0表示在第一个位置找到'e'。

阅读更多: String.IndexOf Method [ ^ ]
When we count things, we start from one, right? That is not what computer programs do, they start from zero. So indices start from 0, which we mortal always think of 1.
To interpret that line of code that you mentioned, we start from the inner then move outward, like this:
1. (word.charAt(i)) gets a character at the index position i (where the character starts from zero, remember) from the word string. Let's say it is 'e'. Now move one step outward.
2. VOWELS.indexOf("e") means to find a "e" inside the VOWELS string and returns its index position (again, it starts from zero), if not found, it returns -1.
3. That explains why using >=0 is correct, because 0 means the 'e' is found at the first position.
Read more: String.IndexOf Method[^]


在这种情况下,要使用的正确运算符为'> ='。



如果您调试代码,如果使用'>',您会看到'a'字符是未拾取的字符。



原因是Java使用基于零的索引,并且String.indexOf将返回字符串中字符的索引,第一个字符将位于索引0处。如果字符无法在字符串中找到,indexOf将返回-1。



因此,如果您真的想使用'。'运算符,请将支票更改为



In in this case, the correct operator to use would be '>='.

If you debug your code, you should see that the 'a' characters are the ones not picked up if you use '>'.

The reason for that is that Java uses zero based indexing, and as String.indexOf will return the index of the character in the string, first character will be at index 0. If the character can not be found in the string, indexOf will return -1.

So, if you really want to use the '.' operator, change the check to

if (VOWELS.indexOf(word.charAt(i)) > -1)
{
    ...
}


第二个单词Squabash寻找第四个字母a。如果你看到这封信的位置,那么位置在VOWELS中是第一位的。



对于这封信[a],VOWELS.indexOf(word.charAt(i))将返回0并且你的支票> 0将失败因此不会计数。



希望这能让你理解。
For the second word Squabash look for the 4th letter a. If you see the position of this letter, a position is first in VOWELS.

For this letter [a] VOWELS.indexOf(word.charAt(i)) will return 0 and your check > 0 will fail hence will not count.

Hope this makes you understand.


这篇关于使用for循环方法打印参数字符串中的元音数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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