作业帮 > 综合 > 作业

使用java编写程序,要求输入一段话,统计其中某个词语出现的次数.

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/04/28 17:32:07
使用java编写程序,要求输入一段话,统计其中某个词语出现的次数.
我是初学者 易懂点的
import java.io.*;
public class Test1 {
public static void main(String[] args) throws IOException{
String source = null;
String key = "";

//从键盘接受输入的一段话
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
source = reader.readLine();
key = "as"; //待统计出现频度的词语

int num = GetFrequency(source, key);

System.out.println(key + " 在这段话中出现的频度为 " + num);
}
public static int GetFrequency(String source,String key){
int i, j, count = 0;
int len1 = source.length(); //这段话的长度
int len2 = key.length(); //待统计词语的长度

for(i=0; i