java解决输入一段字符串,统计其中有多少个单词。(单词用空格隔开)

2024-12-30 03:18:13
推荐回答(3个)
回答1:

import java.util.Scanner;

public class danci {
     public static void main(String[] args) {
       int words=0;
        System.out.println("请输入字符串:");
         Scanner sca =new Scanner(System.in);
         String s1= sca.nextLine();
          System.out.println(s1);
            String[] split=s1.split(" ");
            System.out.println("长度为:"+split.length);
     }
}

回答2:

package danci;
import java.util.Scanner;
public class danci {
public static void main(String[] args) {
int words=0;
System.out.println("请输入字符串:");
Scanner sca =new Scanner

回答3:

你最后打印错了 应该是split.length !!!