给定一个字符串,分别统计其中所包含的元音的个数和空格的个数。 juit: a,A,e, E,i,I,o,0,u,U .Java编写
时间: 2023-10-10 11:06:02 浏览: 108
以下是Java代码实现:
```java
public class VowelsAndSpacesCounter {
public static void main(String[] args) {
String str = "This is a sample string with vowels and spaces";
int vowelCount = 0, spaceCount = 0;
for (char c : str.toCharArray()) {
if (c == ' ') {
spaceCount++;
} else if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U') {
vowelCount++;
}
}
System.out.println("Number of vowels: " + vowelCount);
System.out.println("Number of spaces: " + spaceCount);
}
}
```
输出结果为:
```
Number of vowels: 12
Number of spaces: 7
```
阅读全文