當前位置:成語大全網 - 新華字典 - java語言編寫壹個String的分詞程序,功能就是計算輸入英文句子的單詞個數

java語言編寫壹個String的分詞程序,功能就是計算輸入英文句子的單詞個數

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Danci {

public static void main(String[] args){

String str = new String();

System.out.print("請輸入壹個英文句子:");

try{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));//獲取鍵盤輸入

str = br.readLine();

}catch(IOException e){

e.printStackTrace();

}

String []s = str.split(" ");//轉換成數組

System.out.println("妳輸入的句子***有單詞 "+s.length+" 個");//s.length獲取數組長度

}

}

//此程序只能獲取壹句話的單詞個數.