打印字符串中唯一元音的数量,Java

打印字符串中唯一元音的数量,Java,java,string,Java,String,我需要找出不同元音的数量。我想出了下面的代码,但它不能区分相同的元音: public static int count_Vowels(String str) { str = str.toLowerCase(); int count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i)

我需要找出不同元音的数量。我想出了下面的代码,但它不能区分相同的元音:

public static int count_Vowels(String str) {
    str = str.toLowerCase();
    int count = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
            count++;
        }
    }
    return count;
}
publicstaticintcount\u元音(stringstr){
str=str.toLowerCase();
整数计数=0;
对于(int i=0;i
您可以使用集合数据结构,只需向集合中添加元音,而不用增加计数器。最后,您可以只返回集合的大小。

您可以使用集合数据结构,而不是增加计数器,只需向集合中添加元音即可。最后,您可以只返回集合的大小。

此计数肯定存在问题。至少是这样。你应该重新思考这一点:

if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                || str.charAt(i) == 'o' || str.charAt(i) == 'u')

            count++;

这个计数肯定有问题。至少是这样。你应该重新思考这一点:

if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                || str.charAt(i) == 'o' || str.charAt(i) == 'u')

            count++;

您可以使用方法
contains

public static int count_Vowels(String str) {
    str = str.toLowerCase();
    int count = 0;
    count += string.contains("a") ? 1 : 0;
    count += string.contains("e") ? 1 : 0;
    count += string.contains("i") ? 1 : 0;
    count += string.contains("o") ? 1 : 0;
    count += string.contains("u") ? 1 : 0;
    return count;
}

您可以使用方法
contains

public static int count_Vowels(String str) {
    str = str.toLowerCase();
    int count = 0;
    count += string.contains("a") ? 1 : 0;
    count += string.contains("e") ? 1 : 0;
    count += string.contains("i") ? 1 : 0;
    count += string.contains("o") ? 1 : 0;
    count += string.contains("u") ? 1 : 0;
    return count;
}

我首先将五个变量(每个元音一个)设置为
0
,迭代输入中的字符,如果找到匹配项,则将相应变量设置为
1
,然后简单地返回所述变量的累积值。像

public static int count_Vowels(String str) {
    int a = 0, e = 0, i = 0, o = 0, u = 0;
    for (char ch : str.toLowerCase().toCharArray()) {
        if (ch == 'a') {
            a = 1;
        } else if (ch == 'e') {
            e = 1;
        } else if (ch == 'i') {
            i = 1;
        } else if (ch == 'o') {
            o = 1;
        } else if (ch == 'u') {
            u = 1;
        }
    }
    return a + e + i + o + u;
}

我首先将五个变量(每个元音一个)设置为
0
,迭代输入中的字符,如果找到匹配项,则将相应变量设置为
1
,然后简单地返回所述变量的累积值。像

public static int count_Vowels(String str) {
    int a = 0, e = 0, i = 0, o = 0, u = 0;
    for (char ch : str.toLowerCase().toCharArray()) {
        if (ch == 'a') {
            a = 1;
        } else if (ch == 'e') {
            e = 1;
        } else if (ch == 'i') {
            i = 1;
        } else if (ch == 'o') {
            o = 1;
        } else if (ch == 'u') {
            u = 1;
        }
    }
    return a + e + i + o + u;
}

我在对守则的评论中作了解释:

public static int count_Vowels(String str) {
        str = str.toLowerCase();
        Set<Character> setOfUsedChars = new HashSet<>(); // Here you store used vowels
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                    || str.charAt(i) == 'o' || str.charAt(i) == 'u') { // if currently checked character is vowel...
                setOfUsedChars.add(str.charAt(i)); // add this vowel to setOfUsedChars
            }
        }
        return setOfUsedChars.size(); // size of this sets is a number of vowels present in input String
}
publicstaticintcount\u元音(stringstr){
str=str.toLowerCase();
Set setOfUseChars=new HashSet();//这里存储使用过的元音
对于(int i=0;i
我在对代码的评论中做了解释:

public static int count_Vowels(String str) {
        str = str.toLowerCase();
        Set<Character> setOfUsedChars = new HashSet<>(); // Here you store used vowels
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                    || str.charAt(i) == 'o' || str.charAt(i) == 'u') { // if currently checked character is vowel...
                setOfUsedChars.add(str.charAt(i)); // add this vowel to setOfUsedChars
            }
        }
        return setOfUsedChars.size(); // size of this sets is a number of vowels present in input String
}
publicstaticintcount\u元音(stringstr){
str=str.toLowerCase();
Set setOfUseChars=new HashSet();//这里存储使用过的元音
对于(int i=0;i
代码中的问题是,您没有计算不同的元音,而是计算字符串中的所有元音。实现此目的的Java-8方法:

public static int countDistinctVowels(String str) {
    str = str.toLowerCase();
    int count = (int) str.chars()                     // get IntStream of chars
            .mapToObj(c -> (char) c)                  // cast to char
            .filter(c -> "aeiou".indexOf(c) > -1)     // remove all non-vowels
            .distinct()                               // keep the distinct values
            .count();                                 // count the values

    return count;
}

还要使用适当的Java命名约定:
countDistinctVowels
,no
count\u Distinct\u元音

代码中的问题是,您没有计算不同的元音,而是计算字符串中的所有元音。实现此目的的Java-8方法:

public static int countDistinctVowels(String str) {
    str = str.toLowerCase();
    int count = (int) str.chars()                     // get IntStream of chars
            .mapToObj(c -> (char) c)                  // cast to char
            .filter(c -> "aeiou".indexOf(c) > -1)     // remove all non-vowels
            .distinct()                               // keep the distinct values
            .count();                                 // count the values

    return count;
}
static void vow(String input){          
    String output=input.toLowerCase();
    int flaga=0,flage=0,flagi=0,flago=0,flagu=0;
    for(int i=0;i<input.length();i++) {
        if((output.charAt(i))=='a' && flaga==0) {
            System.out.print(input.charAt(i)+" ");
            flaga++;
        }
        if(output.charAt(i)=='e' && flage==0) {
            System.out.print(input.charAt(i)+" ");
            flage++;
        }
        if(output.charAt(i)=='i' && flagi==0) {
            System.out.print(input.charAt(i)+" ");
            flagi++;
        }
        if(output.charAt(i)=='o' && flago==0) {
            System.out.print(input.charAt(i)+" ");
            flago++;
        }
        if(output.charAt(i)=='u' && flagu==0) {
            System.out.print(input.charAt(i)+" ");
            flagu++;
        }
    }
}
还要使用适当的Java命名约定:
countDistinctVowels
,no
count\u Distinct\u元音

静态void-vow(字符串输入){
static void vow(String input){          
    String output=input.toLowerCase();
    int flaga=0,flage=0,flagi=0,flago=0,flagu=0;
    for(int i=0;i<input.length();i++) {
        if((output.charAt(i))=='a' && flaga==0) {
            System.out.print(input.charAt(i)+" ");
            flaga++;
        }
        if(output.charAt(i)=='e' && flage==0) {
            System.out.print(input.charAt(i)+" ");
            flage++;
        }
        if(output.charAt(i)=='i' && flagi==0) {
            System.out.print(input.charAt(i)+" ");
            flagi++;
        }
        if(output.charAt(i)=='o' && flago==0) {
            System.out.print(input.charAt(i)+" ");
            flago++;
        }
        if(output.charAt(i)=='u' && flagu==0) {
            System.out.print(input.charAt(i)+" ");
            flagu++;
        }
    }
}
字符串输出=input.toLowerCase(); intflaga=0,flage=0,flagi=0,flago=0,flagu=0; 对于(inti=0;i
staticvoidvow(字符串输入){
字符串输出=input.toLowerCase();
intflaga=0,flage=0,flagi=0,flago=0,flagu=0;
对于(int i=0;i
publicstaticvoidmain(字符串args[]){
串句;
int v=0,c=0,ws=0;
扫描仪sc=新的扫描仪(System.in);
句子=sc.nextLine();
sc.close();
句子。toLowerCase();
字符串res=“”;
对于(int i=0;i
publicstaticvoidmain(字符串args[]){
串句;
int v=0,c=0,ws=0;
扫描仪sc=新的扫描仪(System.in);
句子=sc.nextLine();
sc.close();
句子。toLowerCase();
字符串res=“”;

例如(int i=0;i当你说“unique”时,你的意思是只计算一个特定的元音一次吗?给出一个输入和输出的例子。我有时会在面试中给出这个任务。没有一个人问过“哪种语言的元音?”是的,同一个元音的各种出现不应该计数给出一个示例输入和所需的输出。输入“aaaeee”输出“2”当你说“唯一”时,你的意思是它只计算一个特定的元音一次吗?给出一个输入和输出的例子。我有时在采访中会给出这个任务。没有一个人问过“哪种语言的元音?”是的,同一个元音的各种出现不应该计算给出一个示例输入和所需的输出。输入“aaaeee”输出“2”Downvoter请解释Downvoter的原因。Downvoter请解释Downvoter的原因。我在等待相同的答案。@JuanCarlosMendoza感谢您的建议。我认为检查集合中是否存在该元音,如果集合中存在该元音,则最终跳过循环比执行代码广告更快将同一个元音添加到集合上并继续。我使用了回滚选项将此答案编辑为其以前的状态。感谢您指出。我正在等待答案中的相同内容。@JuanCarlosMendoza感谢您的建议。我认为检查集合中该元音的存在并最终跳过循环,以防集合中存在该元音比将同一元音添加到集合on和on的代码执行速度更快。我使用了回滚选项将此答案编辑为其以前的状态。感谢您指出这一点。@ElliotFrisch,您的解决方案当然是非常有效的。我只是