java字符串练习.输入一段英文短文统计

问题描述:

1.统计有多少个单词
2.统计以字母 w 开头的单词数;

  charAt(0)=w  或者 startsWith(w)

3.统计单词中含“or”字符串的单词数;

  indexOf(“or”)!=-1

4.统计长度为 3 的单词数。

 Length()==3


那你是没思路还是?

目的是这个?




 public static void main(String[] args) {
        String sentence = "count word with some condition";
        String[] s = sentence.split(" ");

        int w =0;
        int or = 0;
        int length =0;
        for (String val : s) {
            if(val.startsWith("w")){
                w++;
            }
            if(val.contains("or")){
                or ++;
            }
            if(val.length()==3){
                length ++;
            }
        }
        System.out.println(w);
        System.out.println(or);
        System.out.println(length);
    }