when I was in the primary school ,I had a unforgetful thing between me and my teacher 通过java实现输入每个单词首字母,就可以找到这些单词。思路大概是用string字符串,通过空格将文章分成一个个字符串,然后检索其中首字母。
a黄金罗奇
浏览 1429回答 2
2回答
_潇潇暮雨
public class Test {
public static int find(String[] arr,String keyword){
int count = 0;
for(String s : arr)
if(s.startsWith(keyword))
++count;
return count;
}
public static void main(String[] args) {
String str = "when I was in the primary school ,I had a unforgetful thing between me and my teacher";
String[] arr = str.split("\\s+,*");
System.out.println(find(arr,"w")); // 2
System.out.println(find(arr,"a")); // 2
System.out.println(find(arr,"h")); // 1
}
}