如何使用java打印两个元音之间的字母

如何使用java打印两个元音之间的字母,java,Java,我有一个字符串S=“我爱班加罗尔”它应该打印两个元音之间的字母,如下所示: 1.v 2.ng 3.1 4.r 注意:如果只有1个字母b/w,2个元音不超过此值,我可以打印 这就是我所尝试的: String a=“我爱班加罗尔”; String[]words=a.split(“”); for(字符串字:字){ for(int i=1;i3){ if(i==word.length()-1){ System.out.println(“跳过”); } else if(checkis元音(word.cha

我有一个字符串S=“我爱班加罗尔”它应该打印两个元音之间的字母,如下所示: 1.v 2.ng 3.1 4.r

注意:如果只有1个字母b/w,2个元音不超过此值,我可以打印

这就是我所尝试的:

String a=“我爱班加罗尔”;
String[]words=a.split(“”);
for(字符串字:字){
for(int i=1;i3){
if(i==word.length()-1){
System.out.println(“跳过”);
}
else if(checkis元音(word.charAt(i))&&!checkis元音(word.charAt(i+1))&&checkis元音(word.charAt(i+2))){
System.out.println(word.charAt(i+1));
}
}
}
}

您尝试的方式不正确,因为

  • 您正在检查长度3或更大,这是不正确的
  • 您正在检查元音、正常字母表、元音,这些元音也是不正确的。例:英国英语
  • 这里有一个解决方法

    String[] words = str.split(" ");
    for (String word : words) {
        int firstVowelIndex = -1;
        for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);
            if (checkIsVowel(ch)) {
                // if vowel index is found again and there exists at least one character between the two vowels
                if (firstVowelIndex != -1 && i - firstVowelIndex != 0) {
                    System.out.println(word.substring(firstVowelIndex + 1, i));
                }
                // vowel index is assigned
                firstVowelIndex = i;
            }
        }
    }
    
    输出:

    v
    ng
    l
    r
    

    最好的方法如下:

  • 查找元音和非元音对
  • result
    设置为非元音
  • 继续查找附加到
    result
  • 遇到元音时,请打印或保存
    result
  • 记住你们是一个元音,回到1并重复,直到单词用尽
  • 确保使用打印语句来帮助调试程序

    v
    ng
    l
    r