这是一个计算元音的代码。如何显示元音而不是仅计算它们?。
System.out.println("Enter the String:");
String text = we.readLine();
int count = 0;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
count++;
}
}
System.out.println("The number of vowels in the given String are: " + count);
答案 0 :(得分:0)
对于替代方案,您可以创建元组的char数组,将String转换为char数组并比较每个索引:
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
String text = "Programming";
for (char c : text.toCharArray()) {
for (char vowel : vowels) {
if (c == vowel) {
System.out.println(text + " contains the vowel: " + vowel);
}
}
}