java中设有一个由10个英文单词构成的字符串数组,要求: 1.统计以字母w开头的单词数; 2.统计单词中含“or”字符串的单词数; 3.统计长度为3的单词数。
时间: 2023-10-23 22:14:39 浏览: 107
可以使用以下代码实现:
```java
public class StringArrayExample {
public static void main(String[] args) {
String[] words = {"world", "wide", "web", "word", "work", "wonder", "wonderful", "worth", "worst", "worry"};
int wCount = 0;
int orCount = 0;
int length3Count = 0;
for (String word : words) {
if (word.startsWith("w")) {
wCount++;
}
if (word.contains("or")) {
orCount++;
}
if (word.length() == 3) {
length3Count++;
}
}
System.out.println("以字母w开头的单词数:" + wCount);
System.out.println("含有or字符串的单词数:" + orCount);
System.out.println("长度为3的单词数:" + length3Count);
}
}
```
运行结果:
```
以字母w开头的单词数:6
含有or字符串的单词数:3
长度为3的单词数:2
```
阅读全文