计算单词数

来源:互联网 发布:北京新华电脑学校java 编辑:程序博客网 时间:2024/05/01 15:35
public class CountWords {    public static void main(String[] args) {        String s = " i am hehao ";        //wordCount(s);        wordCount1(s);    }    public static void wordCount(String str){        String[]s1 = str.trim().split(" ");        System.out.println(s1.length);    }    public static void wordCount1(String str){        int word = 0;        int count = 0;        for(int j=0;j< str.length();j++){            if(str.charAt(j)==' '){                word = 0;            }            else if(word==0){                word=1;                count++;            }        }        System.out.println(count);    }}
原创粉丝点击