when I was in the primary school ,I had a unforgetful thing between me and my teacher 通过java实现输入每个单词首字母,就可以找到这些单词。思路大概是用string字符串,通过空格将文章分成一个个字符串,然后检索其中首字母。
2 回答
已采纳
_潇潇暮雨
TA贡献646条经验 获得超225个赞
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 } }
添加回答
举报
0/150
提交
取消