如何检查数组是否包含特定数量的值?

import java.util.Scanner;


public class CountVowel{

    public static void main(String[] args){

        Scanner scan = new Scanner(System.in);

获取数组的大小:


        System.out.println("Type how many words will be typed: ");

        int input = scan.nextInt();    

用字符串值填充数组


        String[] ar1 = new String[input];

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

            System.out.println("Type the elements of array with words: ");

            ar1[i] = scan.next();      

        }

程序的输出:


        System.out.println( input + " words are typed and " + 

        countVowels(ar1) + 

         " of them contain more than 3 vowels.");

    }

计算元音的方法:


    public static int countVowels(String[] ar1){  // this method counts 


        int a = 0;

        String[] ar2 = new String[]{"a", "e", "i", "u", "y", "o"};

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

            for(String s : ar2){

                if(ar1[i].toLowerCase().contains(s)){

                    a++;

                }

            }

        }

        return a;

    }

}

上面的方法是检查元音,但我不知道如何让它检查是否有超过3个元音。


森林海
浏览 184回答 5
5回答

狐的传说

另一种解决replaceAll方法。主要思想是从word.length()没有元音的相同词长中减去。并检查差异。public static int countVowels(String[] ar1){&nbsp; &nbsp; int a = 0;&nbsp; &nbsp; for (String word : ar1) {&nbsp; &nbsp; &nbsp; &nbsp; int i = word.length() - word.toLowerCase().replaceAll("[aeyiuo]", "").length();&nbsp; &nbsp; &nbsp; &nbsp; if (i >= 3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return a;}或者您可以matches()按照@pkgajulapalli 的建议使用。使用流 api 可以非常简洁:long count = Arrays.stream(words)&nbsp; &nbsp; &nbsp; &nbsp; .filter(s -> s.toLowerCase().matches("(.*[aeyiuo].*){3,}"))&nbsp; &nbsp; &nbsp; &nbsp; .count();

梵蒂冈之花

public static int countVowels(String[] ar1) { // this method counts&nbsp; &nbsp; int vowelPerWord = 0;&nbsp; &nbsp; int totalWordsWithThreeVowels = 0;&nbsp; &nbsp; char[] ar2 = new char[] { 'a', 'e', 'i', 'u', 'y', 'o' };&nbsp; &nbsp; for (int i = 0; i < ar1.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; vowelPerWord = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < ar1[i].length(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int k = 0; k < ar2.length; k++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ar2[k] == (ar1[i].charAt(j))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vowelPerWord++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (vowelPerWord >= 3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalWordsWithThreeVowels++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return totalWordsWithThreeVowels;}编辑好吧,现在我修复了错误并编辑了变量名以使其更有意义。虽然这是 O(n*m) 我相信(其中 n 是字符串数,m 是最长字符串的字符数)(不太复杂)它可以完成工作 ar1 在这种情况下是您的输入字符串,ar2 只是存在的元音。因此,您遍历 ar1 中的每个字符串并将“vowelPerWord”设置为 0,遍历每个字符串中的每个字符并检查它是否是元音,将 vowelPerWord 增加 1。最后,在您遍历该字符串的每个字符之后您检查是否有 3 个或更多元音,如果有,则增加 totalWordsWithThreeVowels,最后返回。

肥皂起泡泡

你需要的是一个额外的循环和计数。像这样的东西:// This method counts how many words have at least 3 vowelspublic static int countVowels(String[] wordsArray){&nbsp; int atLeastThreeVowelsCount = 0;&nbsp; for(String word : wordsArray){&nbsp; &nbsp; int vowelCount = 0;&nbsp; &nbsp; for(String vowel : new String[]{ "a", "e", "i", "u", "y", "o" }){&nbsp; &nbsp; &nbsp; if(word.toLowerCase().contains(vowel)){&nbsp; &nbsp; &nbsp; &nbsp; vowelCount++;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(vowelCount >= 3){&nbsp; &nbsp; &nbsp; atLeastThreeVowelsCount++;&nbsp; &nbsp; }&nbsp; }&nbsp; return atLeastThreeVowelsCount;}请注意,我还为变量提供了一些更有用的名称,而不是ar1,s等,因此更容易阅读正在发生的事情。

慕标5832272

您可以使用正则表达式匹配来查找字符串是否包含任何字符集。例如,如果要查找字符串是否包含任何元音,可以使用:String str = "yydyrf";boolean contains = str.toLowerCase().matches(".*[aeiou].*");System.out.println(contains);编辑:所以你的代码看起来像:public static int countVowels(String[] ar1) {&nbsp; &nbsp; int a = 0;&nbsp; &nbsp; String[] ar2 = new String[] { "a", "e", "i", "u", "y", "o" };&nbsp; &nbsp; String pattern = ".*[" + String.join("", ar2) + "].*";&nbsp; &nbsp; for (int i = 0; i < ar1.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (ar1[i].matches(pattern)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return a;}

Smart猫小萌

public static int countVowels(String[] ar1){&nbsp; // this method counts&nbsp;&nbsp; &nbsp; //Create hash map key = array string && value = vowels count&nbsp;&nbsp; &nbsp; Map<String,Integer> mapVowels=new HashMap<String,Integer>();&nbsp; &nbsp; int a = 0;&nbsp; &nbsp; String[] ar2 = new String[]{"a", "e", "i", "u", "y", "o"};&nbsp; &nbsp; for(int i = 0; i < ar1.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; for(String s : ar2){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(ar1[i].toLowerCase().contains(s)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Check map string already has vowel count then increase by one&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(mapVowels.get(s)!=null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mapVowels.put(s,mapVowels.get(s)+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//After add the vowels count get actual count and check is it more than 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(mapVowels.get(s)>3)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //If the vowels string new for map then add vowel count as 1 for first time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapVowels.put(s,1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return a;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java