早教吧 育儿知识 作业答案 考试题库 百科 知识分享

java中输入一段英文文章,请统计出该文章中的冠词数有多少个.输入一段英文文章,请统计出该文章中的冠词数有多少个。英文中的冠词是指a、an和the

题目详情
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;
    }
}

这么简单的代码,不需要解释,看得懂的吧。

看了 java中输入一段英文文章,...的网友还看了以下: