example0401_str.java

来自「本程序实现下列功能:统计一段英文短文中出现的各种冠词的次数 单词之间的分隔符」· Java 代码 · 共 33 行

JAVA
33
字号
/* 本程序实现下列功能:统计一段英文短文中出现的各种冠词的次数
 * 单词之间的分隔符有:回车、空格、逗号、句号、叹号、问号
 */
 
 import java.util.*;
 
 public class Example0401_Str
 {
 	public static void main(String[] args)
 	{
 		String para = "I am a teacher.\n A an anan the The An?"; //此处赋值为一段英文短文字符串
 		String delim = "\n ,.!?"; //定义分隔符集
 		StringTokenizer strToken = new StringTokenizer(para, delim);
 		int c1 = 0;
 		int c2 = 0;
 		int c3 = 0;
 		
 		while (strToken.hasMoreTokens())
 		{
 			String token = strToken.nextToken().toLowerCase();
 			
 			if (token.equals("a")) c1++;
 			
 			if (token.equals("an")) c2++;
 			
 			if (token.equals("the")) c3++;
 		}
 		
 		System.out.println("a = " + c1);
 		System.out.println("an = " + c2);
 		System.out.println("the = " + c3);
 	}
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?