JAVA程序,帮忙检查,输入string数元音字母,请仔细检查下,谢谢

2025-04-06 14:53:44
推荐回答(1个)
回答1:



public static void main(String[] args) {
String str;
int Count = 0, aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;

System.out.println("Enterthe string");
str = Keyboard.readString();
                if (!(str == null||"".equals(str))) {//加入这个判断吧。养成良好习惯,字符串在用之前一定要判断是否为空。
for (int index = 0; index < str.length(); index++) {
if (str.charAt(index) == 'a')
aCount++;
else if (str.charAt(index) == 'e')
eCount++;
else if (str.charAt(index) == 'i')
iCount++;
else if (str.charAt(index) == 'o')
oCount++;
else if (str.charAt(index) == 'u')
uCount++;
}
}
Count = aCount + eCount + iCount + oCount + uCount;

System.out.println("Thereare " + Count + " vowels in this string");
}
}