作业帮 > 综合 > 作业

java中输入一段英文文章,请统计出该文章中的冠词数有多少个.

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/09/29 21:58:05
java中输入一段英文文章,请统计出该文章中的冠词数有多少个.
输入一段英文文章,请统计出该文章中的冠词数有多少个。
英文中的冠词是指a、an和the
package play;

import java.util.Scanner;

public class Test2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一段文章:");
        String source = sc.nextLine();
        System.out.println(source);
        int aCount = statCount(source, "a");
        int anCount = statCount(source, "an");
        int theCount = statCount(source, "the");
        System.out.println("这段文章中,冠词个数为:" + aCount + anCount + theCount);
    }

    private static int statCount(String source, String express) {
        int count = 0;
        if ((source.startsWith(express + " ") && source.length() > 2)) {
            count++;
        }
        if ((source.endsWith(" " + express) && source.length() > 2)) {
            count++;
        }
        String temp = source;
        String _express = " " + express + " ";
        int site = temp.indexOf(_express);
        while (site != -1) {
            count++;
            temp = temp.substring(site + 1, temp.length());
            site = temp.indexOf(_express);
        }
        return count;
    }
}这么简单的代码,不需要解释,看得懂的吧。