1. 程式人生 > >jmu-Java&Python-統計一段文字中的單詞個數並按單詞的字母順序排序後輸出

jmu-Java&Python-統計一段文字中的單詞個數並按單詞的字母順序排序後輸出

現需要統計若干段文字(英文)中的不同單詞數量。
如果不同的單詞數量不超過10個,則將所有單詞輸出(按字母順序),否則輸出前10個單詞。

注1:單詞之間以空格(1個或多個空格)為間隔。
注2:忽略空行或者空格行。
注3:單詞大小寫敏感,即'word'與'WORD'是兩個不同的單詞 。

輸入說明

若干行英文,最後以!!!!!為結束。

輸出說明

不同單詞數量。 然後輸出前10個單詞(按字母順序),如果所有單詞不超過10個,則將所有的單詞輸出。

輸入樣例

Failure is probably the fortification in your pole
It is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta
when you Are wondering whether new money it has laid
background Because of you, then at the heart of the
most lax alert and most low awareness and left it
godsend failed
!!!!!

輸出樣例

49
Are
Because
Failure
It
a
alert
and
are
as
at
import java.util.*;

public class Main{

	public static void main(String[] args) {
		Set<String> s=new TreeSet<String>();
		Scanner scan=new Scanner(System.in);
		String text = "",temp;
		while(true) {
			temp=scan.next();
			if(temp.equals("!!!!!"))break;
			s.add(temp);
		}
		
		System.out.println(s.size());
		Iterator<String> i=s.iterator();
		if(s.size()<10) {
			while(i.hasNext()) {
				System.out.println(i.next());
			}
		}
		else {
			for(int j=0;j<10;j++) {
				System.out.println(i.next());
			}
		}
		scan.close();
	}
}