输入一字符串,统计其中有多少个单词(单词之间用空格分隔)(java)

import java.util.*;
class Example3{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();//这里的sc.nextLine()空格也会记数;
		StringTokenizer st = new StringTokenizer(s," ");
		int numble = st.countTokens();
		while(st.hasMoreTokens())
		{
			String str = st.nextToken();
		}
		System.out.print(numble);
	}
}

你可能感兴趣的:(Java例题与应用)