java高频词汇统计代码

2023-11-24 11:01:44 浏览数 (1)

代码语言:javascript复制
// 词频统计
class CountWords {
    public static int count() {
        int count = 0;
        System.out.println("请输入英文句子");
        Scanner r = new Scanner(System.in);
        String str = r.nextLine();
//        分割子串
        String arr[] = str.split("[',','.','?',' ','!',';',' ']");
//        for (String c : arr) {
//            System.out.print(c);
//        }
        System.out.println("请输入统计的单词");
        String wd = r.nextLine();
        for (int i = 0; i < arr.length; i  ) {
            System.out.print('n'   arr[i]);
            if (wd.equals(arr[i])) {
                count  ;
            }
        }
        return count;
    }
}

0 人点赞