数数大多数元音Java

时间:2019-01-07 19:25:09

标签: java

这是一个逻辑错误,我如何才能找到用户输入的最多元音句子。

import java.util.Scanner;

public class ZanoreShqip {

    public static void main(String[]args) {

        Scanner input = new Scanner(System.in);
        String fjalia;
        int count_zanore = 0;
        int maxVowelCount = 0;
        String wordWithMostVowels = "";
        final String SENTINEL = "FUND";
        System.out.print("Sheno fjali dhe fund per te perfunduar : ");
        fjalia=input.nextLine();
        while(!fjalia.equalsIgnoreCase(SENTINEL)) {
            fjalia = fjalia.toLowerCase();
            for(int i = 0; i <fjalia.length(); ++i) {
                char ch = fjalia.charAt(i);
                if (ch == 'a' || ch == 'e' || ch == 'i' 
                || ch == 'o' || ch == 'u') {
                    count_zanore++;
                } if ( count_zanore > maxVowelCount) {
                    wordWithMostVowels = fjalia;
                    maxVowelCount = count_zanore;
                }
            }
            System.out.print("\nSheno fjali dhe fund per te perfunduar : ");
            fjalia = input.nextLine();
        }
        System.out.println(wordWithMostVowels);
        System.out.println(maxVowelCount);
    }
}

1 个答案:

答案 0 :(得分:1)

您忘记了这部分count_zanore = 0;

尝试一下

while(!fjalia.equalsIgnoreCase(SENTINEL)){
   fjalia = fjalia.toLowerCase();
   for(int i = 0; i <fjalia.length(); ++i)
   {
      char ch = fjalia.charAt(i);
       if (ch == 'a' || ch == 'e' || ch == 'i'
                    || ch == 'o' || ch == 'u'){
          count_zanore++;
       }
       if ( count_zanore > maxVowelCount){
                wordWithMostVowels = fjalia;
                maxVowelCount = count_zanore;

       }

   }
   count_zanore = 0;
   System.out.print("\nSheno fjali dhe fund per te perfunduar : ");
   fjalia = input.nextLine();
}