JAVA给定一个字符串,分别统计其中所包含的元音的个数和空格的个数
时间: 2023-06-13 16:07:30 浏览: 84
java基础编程:统计出给定字符串的英文字母、空格、数字和其它字符的个数
下面是JAVA代码实现:
```java
public class Main {
public static void main(String[] args) {
String str = "Hello World";
int vowelCount = 0;
int spaceCount = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
vowelCount++;
} else if (c == ' ') {
spaceCount++;
}
}
System.out.println("Vowel count: " + vowelCount);
System.out.println("Space count: " + spaceCount);
}
}
```
输出结果为:
```
Vowel count: 3
Space count: 1
```
阅读全文