Java输入一系列英文单词统计输出小于等于6个字母的单词,以及大于6个字母的单词个数

Java输入一系列英文单词统计输出小于等于6个字母的单词,以及大于6个字母的单词个数

输入一串字符串,进行分割转为字符串数组,然后求长度。

import java.util.Scanner;
public class Demo {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String str=in.nextLine();
        String[] strs = str.split(" ");
        int count1=0,count2=0;
        for (String s: strs) {
            if (s.length()<=6){
                count1++;
            }else {
                count2++;
            }
        }
        System.out.println("小于等于6个字母的单词数:"+count1);
        System.out.println("大于6个字母的单词数:"+count2);
    }
}

img