Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 指出哪个元音在字符串中出现最多_Java_String_Algorithm_Find Occurrences - Fatal编程技术网

Java 指出哪个元音在字符串中出现最多

Java 指出哪个元音在字符串中出现最多,java,string,algorithm,find-occurrences,Java,String,Algorithm,Find Occurrences,我正在用Java编写一个程序,该程序应该给出如下输出: 元音=8 上限=2 数字=5 空格=6 元音i出现最多=4 我的代码经过编译,除了确定哪个元音出现得最多外,我在所有方面都取得了成功 我不知道我应该怎么做,首先计算单个元音(比如“a”)出现的次数(而不是字符串中出现的总元音数)。在我找到每个元音的总数后,我不确定用什么来确定最大值的元音。一旦我能够得到这两个步骤,我就不确定如何正确地输出。我更愿意用if语句来完成这一点,但是,我不知道这是否可行 任何帮助/提示都将不胜感激,以下是我编写

我正在用Java编写一个程序,该程序应该给出如下输出:

  • 元音=8
  • 上限=2
  • 数字=5
  • 空格=6
  • 元音i出现最多=4
我的代码经过编译,除了确定哪个元音出现得最多外,我在所有方面都取得了成功

我不知道我应该怎么做,首先计算单个元音(比如“a”)出现的次数(而不是字符串中出现的总元音数)。在我找到每个元音的总数后,我不确定用什么来确定最大值的元音。一旦我能够得到这两个步骤,我就不确定如何正确地输出。我更愿意用if语句来完成这一点,但是,我不知道这是否可行

任何帮助/提示都将不胜感激,以下是我编写的代码:

    // which vowel occurs the most
    if (ch == 'a')
      vowelA++;
    else if (ch == 'e')
      vowelE++;
    else if (ch == 'i')
      vowelI++;
    else if (ch == 'o')
      vowelO++;
    else if (ch == 'u')
      vowelU++;

    if (vowelA > vowelE && vowelA > vowelI && vowelA > vowelO && vowelA > vowelU)
    {
      maxVowels = vowelA;
    }
  }

// OUTPUT
System.out.println("vowel" + " " + "occurs the most = " + maxVowels);

 }
}

有很多方法。我正在写一本。你可以试试:

// for number of VOWELS
for (int i = 0; i < str.length(); i++)
{
    ch = str.charAt(i);
    ch = Character.toLowerCase(ch);

    // is this a vowel
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
    {
        vowels++;
    }

    // which vowel occurs the most
    if (ch == 'a')
        vowelA++;
    else if (ch == 'e')
        vowelE++;
    else if (ch == 'i')
        vowelI++;
    else if (ch == 'o')
        vowelO++;
    else if (ch == 'u')
        vowelU++;

}

maxVowels = Math.max(vowelA, Math.max(vowelE, Math.max(vowelI, Math.max(vowelO, vowelU))));
//表示元音的数量
对于(int i=0;i
输出:

注意:我刚刚添加了
maxvouels=Math.max(元音la,Math.max(元音,Math.max(元音li,Math.max(元音lo,元音lu)))
在大写逻辑之前,如果存储MAXVOMELS值的条件已被删除

另一种方法:替换
maxvouels=Math.max(元音la,Math.max(元音,Math.max(元音li,Math.max(元音lo,元音lu)))
Collections.max(Arrays.asList(元音、元音、元音、元音、元音)一起使用

请尝试以下代码:

{
   public static void main (String []args)
 {
    Scanner scan = new Scanner(System.in);
    String str = scan.nextLine();

    int vowels = 0, digits = 0, spaces = 0, upper = 0;
    String line = str;
    int max_vowel_count = 0;
    for(int i = 0; i < line.length(); ++i)
    {
        char ch = line.charAt(i);
        if(ch == 'a' || ch == 'e' || ch == 'i'
            || ch == 'o' || ch == 'u') {


            int a_count = line.length() - line.replace("a", "").length();
            int e_count = line.length() - line.replace("e", "").length();
            int i_count = line.length() - line.replace("i", "").length();
            int o_count = line.length() - line.replace("o", "").length();
            int u_count = line.length() - line.replace("u", "").length();

            max_vowel_count = Math.max(a_count, Math.max(e_count, Math.max(i_count, Math.max(o_count, u_count))));

            ++vowels;
        }
        else if( ch >= '0' && ch <= '9')
        {
            ++digits;
        }
        else if (ch >= 'A' && ch <= 'Z'){ 
            ++upper; 
        }
        else if (ch ==' ')
        {
            ++spaces;
        }
    }

    System.out.println("Vowels: " + vowels);
    System.out.println("Digits: " + digits);
    System.out.println("Upper Case: " + upper);
    System.out.println("White spaces: " + spaces);
    System.out.println("Max Vowel Count "+ max_vowel_count);

}
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
String str=scan.nextLine();
int元音=0,数字=0,空格=0,上限=0;
字符串行=str;
int max_元音计数=0;
对于(int i=0;i如果您对基于流的解决方案感兴趣,请执行以下操作:


公共静态void main(字符串[]args){
String input=“此字符串包含元音和12345位数字”;
映射计数=input.chars()
.mapToObj((str)->图表类型(str))
.flatMap((it)->it)
.collect(Collectors.toMap(Function.identity(),v->1,Integer::sum));
System.out.println(“元音:”+counts.getOrDefault(“元音”,0));
System.out.println(“大写:+counts.getOrDefault(“大写”,0));
System.out.println(“数字:”+counts.getOrDefault(“数字”,0));
System.out.println(“空白:+counts.getOrDefault(“空白”,0));
可选的max元音=counts.entrySet()
.stream()
.filter((str)->str.getKey().length()==1)
.filter((str)->元音.contains((int)str.getKey().charAt(0)))
.max(Comparator.comparingit(Map.Entry::getValue));
if(maxvouels.isPresent()){
System.out.println(“最大元音为”+Max元音.get().getKey()+”和“+Max元音.get().getValue()+”出现次数”);
}否则{
System.out.println(“未找到元音”);
}
}
私有静态集合元音=集合((int)'a',(int)'e',(int)'i',(int)'o',(int)'u');
公共静态布尔IS元音(int c){
返回元音。包含(字符。toLowerCase(c));
}
公共静态流图表类型(INTC){
列表类型=新的ArrayList();
if(是元音(c)){
类型。添加(“元音”);
添加(String.valueOf((char)c));
}
if(字符.isWhitespace(c)){
类型。添加(“空白”);
}
if(字符isDigit(c)){
类型。添加(“数字”);
}
if(字符.大写(c)){
类型。添加(“上限”);
}
返回类型。stream();
}

(将元音类型设置为枚举将是一种改进)

我已经添加了一个答案。请您尝试一下,并让我知道它是否适合您吗?@Nitin Bisht非常感谢!!!我欠您的。这是困难的部分,现在我所要做的就是计算输出(应该不会太困难)是的,它需要在许多方面进行修改才能看起来更好。非常感谢!!这帮了我很大的忙。现在我要弄清楚的是如何正确地编写System.out.println语句而不是链式的
Math.max
调用,你只需执行
Collections.max(Arrays.asList(元音,元音,元音,元音,元音,元音,元音,元音)
@Fureeish你是对的。我已经提到了很多方法。这也是其中之一。错误“私有静态集合元音=Set.of((int)'a',(int)'e',(int)'I',(int)'o',(int)'u');line.Set.of是在Java 9中添加的