gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:21:29 25 4
gpt4 key购买 nike

我有一个字符串 S=“我爱类加罗尔” 它应该打印 2 个元音之间的字母,如下所示:1.v2.ng3.l4.r

注意:如果只有 1 个字母黑白 2 个元音不超过这个数量,我就可以打印。

这是我尝试过的:

String a = "i love bangalore";

String[] words = a.split(" ");
for (String word : words) {
for (int i = 1; i < word.length(); i++) {
if (word.length() > 3) {
if(i==word.length()-1){
System.out.println("skip");
}
else if(checkIsVowel(word.charAt(i))&&!checkIsVowel(word.charAt(i+1))&&checkIsVowel(word.charAt(i+2))){
System.out.println(word.charAt(i+1));
}

}
}
}

最佳答案

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

  1. 您正在检查长度是否为 3 或以上,但事实并非如此
  2. 您正在检查元音、正常字母、元音,这也是不正确的。例如:英语

这是解决这个问题的一种方法

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;
}
}
}

输入:

i love bangalore

输出:

v
ng
l
r

关于java - 如何使用java打印两个元音之间的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59518852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com