为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

森林海 2022-06-15 15:17:29
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个元音。
查看完整描述

5 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

另一种解决replaceAll方法。主要思想是从word.length()没有元音的相同词长中减去。并检查差异。


public static int countVowels(String[] ar1){

    int a = 0;

    for (String word : ar1) {

        int i = word.length() - word.toLowerCase().replaceAll("[aeyiuo]", "").length();

        if (i >= 3) {

            a++;

        }

    }

    return a;

}

或者您可以matches()按照@pkgajulapalli 的建议使用。使用流 api 可以非常简洁:


long count = Arrays.stream(words)

        .filter(s -> s.toLowerCase().matches("(.*[aeyiuo].*){3,}"))

        .count();


查看完整回答
反对 回复 2022-06-15
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

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


    int vowelPerWord = 0;

    int totalWordsWithThreeVowels = 0;

    char[] ar2 = new char[] { 'a', 'e', 'i', 'u', 'y', 'o' };

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

        vowelPerWord = 0;

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

            for (int k = 0; k < ar2.length; k++) {

                if (ar2[k] == (ar1[i].charAt(j))) {

                    vowelPerWord++;

                }

            }

        }

        if (vowelPerWord >= 3) {

            totalWordsWithThreeVowels++;

        }

    }

    return totalWordsWithThreeVowels;

}

编辑


好吧,现在我修复了错误并编辑了变量名以使其更有意义。虽然这是 O(n*m) 我相信(其中 n 是字符串数,m 是最长字符串的字符数)(不太复杂)它可以完成工作 ar1 在这种情况下是您的输入字符串,ar2 只是存在的元音。


因此,您遍历 ar1 中的每个字符串并将“vowelPerWord”设置为 0,遍历每个字符串中的每个字符并检查它是否是元音,将 vowelPerWord 增加 1。最后,在您遍历该字符串的每个字符之后您检查是否有 3 个或更多元音,如果有,则增加 totalWordsWithThreeVowels,最后返回。


查看完整回答
反对 回复 2022-06-15
?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

你需要的是一个额外的循环和计数。像这样的东西:


// This method counts how many words have at least 3 vowels

public static int countVowels(String[] wordsArray){

  int atLeastThreeVowelsCount = 0;

  for(String word : wordsArray){

    int vowelCount = 0;

    for(String vowel : new String[]{ "a", "e", "i", "u", "y", "o" }){

      if(word.toLowerCase().contains(vowel)){

        vowelCount++;

      }

    }

    if(vowelCount >= 3){

      atLeastThreeVowelsCount++;

    }

  }

  return atLeastThreeVowelsCount;

}



请注意,我还为变量提供了一些更有用的名称,而不是ar1,s等,因此更容易阅读正在发生的事情。


查看完整回答
反对 回复 2022-06-15
?
慕标5832272

TA贡献1966条经验 获得超4个赞

您可以使用正则表达式匹配来查找字符串是否包含任何字符集。例如,如果要查找字符串是否包含任何元音,可以使用:


String str = "yydyrf";

boolean contains = str.toLowerCase().matches(".*[aeiou].*");

System.out.println(contains);

编辑:


所以你的代码看起来像:


public static int countVowels(String[] ar1) {

    int a = 0;

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

    String pattern = ".*[" + String.join("", ar2) + "].*";

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

        if (ar1[i].matches(pattern)) {

            a++;

        }

    }

    return a;

}


查看完整回答
反对 回复 2022-06-15
?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

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

    //Create hash map key = array string && value = vowels count 

    Map<String,Integer> mapVowels=new HashMap<String,Integer>();

    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)){

                //Check map string already has vowel count then increase by one

                if(mapVowels.get(s)!=null) {

                 mapVowels.put(s,mapVowels.get(s)+1);

                 //After add the vowels count get actual count and check is it more than 3

                 if(mapVowels.get(s)>3)

                     a++;

                }

                else {

                    //If the vowels string new for map then add vowel count as 1 for first time

                    mapVowels.put(s,1);

                }

            }

        }

    }

    return a;

}


查看完整回答
反对 回复 2022-06-15
  • 5 回答
  • 0 关注
  • 183 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号