Java oop:查找字符"a"在字符串"What happend today"中出现的次数

//构建一个java程序,查找字符"a"在字符串"What happend today"中出现的次数。
package a;
public class Student{
public static void main(String[] args) {
String str="What happend today";
int times = searchstr("a", str); //返回2
System.out.println("字符a在字符串What happend today中出现的次数:"+times); /*times就是次数的意思*/
}
public static int searchstr(String key/*要搜索的关键词*/, String str/*字符串*/) {
int index = 0;//每次的搜索到的下标
int count = 0;//计数器
while (( index=str.indexOf(key, index)) != -1) {
index = index + key.length()/*关键词的长度*/;
count++;
}
return count; /*没void就要写return,这是规定*/
}
}
