计数元音,重复方法

时间:2014-10-22 20:24:18

标签: java bluej

    public static void main(String args[])
     {
        Scanner sc = new Scanner(System.in);
        int countVowel=0;
        int countVowelA=0;
        int countVowelE=0;
        int countVowelI=0;
        int countVowelO=0;
        int countVowelU=0;
        char ch;
        String str;
        System.out.println("Please enter the string : ");
        str = sc.nextLine();
        for(int i = 0; i<=str.length(); i ++)
        {
            ch = str.charAt(i);
            if(ch == 'a' || ch =='A')
            {
                countVowelA++;                 
                countVowel++;
            }
            if(ch == 'e' || ch =='E')
            {
                countVowelE++;
                countVowel++;
            }
            if(ch == 'i' || ch =='I')
            {
                countVowelI++;
                countVowel++;
            }
            if(ch == 'o' || ch =='O')
            {
                countVowelO++;
                countVowel++;
            }
            if(ch == 'u' || ch =='U')
            {
                countVowelU++;
                countVowel++;
            }
            i++;
        }
        System.out.println("Occurances of A in given string  : " +countVowelA);
        System.out.println("Occurances of E in given string  : " +countVowelE);
        System.out.println("Occurances of I in given string  : " +countVowelI);
        System.out.println("Occurances of O in given string  : " +countVowelO);
        System.out.println("Occurances of U in given string  : " +countVowelU);
        System.out.println("Number of vowels in strings are  : " +countVowel);
    }
}

对我来说,我遇到了麻烦,让我们举个例子,如果我输入lebron james是最好的篮球运动员,你就知道了。它给了我一个错误,它也不算所有的元音?另外,你可以告诉我的代码是否正确

3 个答案:

答案 0 :(得分:1)

如评论中所述,您的循环变量i会递增两次。一旦进入for语句本身,另一个进入循环结束。

这意味着计数器变为:0,2,4,6而不是0,1,2,3。

这会给你错误的答案。

但是,错误的原因不是这个,而是您在i <= str.length()之前检查条件,而不是i < str.length()。字符串中的字符,例如3个字符,如&#34;&#34;是0,1,2。没有字符编号3.所以当我等于str.length时,你会得到一个错误。

答案 1 :(得分:1)

检查下面的行

 for(int i = 0; i<=str.length(); i ++)

更改为

 for(int i = 0; i<str.length(); i ++)

为什么?

因为在Java中,索引从零开始。当您拥有i <= str.length时,它超出了字符串的范围索引,并为您提供java.lang.StringIndexOutOfBoundsException

另一个问题,您已将变量i 增加两倍。如果条款完全没有必要,那么即使你纠正了边界问题,它也会给你错误的答案。

答案 2 :(得分:0)

试试此代码

import java.util.Scanner;
public class CountVowels {

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int countVowel=0;
        int countVowelA=0;
        int countVowelE=0;
        int countVowelI=0;
        int countVowelO=0;
        int countVowelU=0;
        char ch;
        String str;
        System.out.println("Please enter the string : ");
        str = sc.nextLine();
        char[] c = str.toCharArray();
        for(int i = 0; i<c.length; i ++)
        {
            if(c[i] == 'a' || c[i] =='A')
            {
                countVowelA++;                 
                countVowel++;
            }
            else if(c[i] == 'e' || c[i] =='E')
            {
                countVowelE++;
                countVowel++;
            }
            else if(c[i] == 'i' || c[i] =='I')
            {
                countVowelI++;
                countVowel++;
            }
            else if(c[i] == 'o' || c[i] =='O')
            {
                countVowelO++;
                countVowel++;
            }
            else if(c[i] == 'u' || c[i] =='U')
            {
                countVowelU++;
                countVowel++;
            }
            //i++;
        }
        System.out.println("Occurances of A in given string  : " +countVowelA);
        System.out.println("Occurances of E in given string  : " +countVowelE);
        System.out.println("Occurances of I in given string  : " +countVowelI);
        System.out.println("Occurances of O in given string  : " +countVowelO);
        System.out.println("Occurances of U in given string  : " +countVowelU);
        System.out.println("Number of vowels in strings are  : " +countVowel);
    }
}