用java语言回答上一个问题
时间: 2024-02-15 22:01:29 浏览: 17
好的,下面是用Java语言来回答您的问题:
1. 统计以字母w开头的单词数:
```java
String sentence = "We love School of Computer Science and Engineering Yulin Normal University!";
String[] words = sentence.split(" "); // 将句子分割成单词
int count = 0;
for (String word : words) {
if (word.startsWith("w") || word.startsWith("W")) { // 判断单词是否以W或w开头
count++;
}
}
System.out.println("以W开头的单词数为:" + count);
```
2. 统计单词中含“o”字符串的单词数:
```java
String sentence = "We love School of Computer Science and Engineering Yulin Normal University!";
String[] words = sentence.split(" "); // 将句子分割成单词
int count = 0;
for (String word : words) {
if (word.indexOf("o") >= 0 || word.indexOf("O") >= 0) { // 判断单词中是否含有o或O
count++;
}
}
System.out.println("含有o的单词数为:" + count);
```
3. 统计长度为3的单词数:
```java
String sentence = "We love School of Computer Science and Engineering Yulin Normal University!";
String[] words = sentence.split(" "); // 将句子分割成单词
int count = 0;
for (String word : words) {
if (word.length() == 3) { // 判断单词长度是否为3
count++;
}
}
System.out.println("长度为3的单词数为:" + count);
```
希望我的回答能够帮到您!