1. java统计单词的个数
import java.util.Scanner;
public class Test40031 {
public static void main(String []args ){
int ri, repeat,count, word,i;
String line;
char c;
Scanner in=new Scanner(System.in);
repeat=(in.nextLine()).charAt(0)-'0'; //输入repeat
for(ri=1; ri<=repeat; ri++){
line=in.nextLine(); //输入一行字符
/*---------*/
count=Count(line);
System.out.println(count);
}
}
public static int Count(String str){
String[] str1=str.split(" ");
int count=str1.length;
return count;
}
}
2. 用java编写应用程序,分析字符串,分别输出字符串的单词,并统计单词个数
public class StringTest {x0dx0a public static void main(String[] args) {x0dx0a //定义一个测试字符串x0dx0a String testStr = "this is a test string";x0dx0a x0dx0a //将该字符串按空格分解辩碰岁成单吵嫌词数组x0dx0a String[] words = testStr.split(" ");x0dx0a x0dx0a //输出单词个数x0dx0a System.out.println("该字符串一共有" + words.length + "个单词!");x0dx0a x0dx0a //携睁输出每一个单词x0dx0a for (int i = 0; i < words.length; i++) {x0dx0a System.out.println("该字符串的第" + (i+1) + "个单词是:" + words[i]);x0dx0a }x0dx0a }x0dx0a}