共回答了17个问题采纳率:94.1%
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;
}
}这么简单的代码,不需要解释,看得懂的吧。
1年前
5