大学学习之java篇
题目:设有一个由10个英文单词构成的字符串数组,要求:(1)统计以字母 w 开头的单词数;(2)统计单词中含“or”字符串的单词数;(3)统计长度为 3 的单词数。
使用工具:
Eclipse IDE for Java Developers - 2019-12
代码如下:
package firstpack;
import java.util.Scanner;
public class text{
public static void main(String[] args) {
String[] words = new String[10];
System.out.println("请输入十个单词:");
Scanner put=new Scanner(System.in);
int fircount = 0,seccount = 0,thicount = 0;
int i = 0;
for(int j = 0;j < words.length;j++) {
words[j] = put.next();
}
do {
if(words[i].charAt(0)=='w') {
fircount++;
}
if(words[i].contains("or")) {
seccount++;
}
if(words[i].length()==3) {
thicount++;
}
i++;
}
while(i<words.length);
System.out.println("以字母w开头的单词数:"+fircount);
System.out.println("单词中含“or”字符串的单词数:"+seccount);
System.out.println("长度为 3 的单词数:"+thicount);
}
}
运行结果: