1. 程式人生 > >輸入一字串,統計其中有多少個單詞(單詞之間用空格分隔)

輸入一字串,統計其中有多少個單詞(單詞之間用空格分隔)

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);
    }
}