使用正则表达式计算所有单词
/*n o w j a v a . c o m - 时 代 Java 提供*/ import java.util.Scanner; public class CountAllWords { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String[] input = scn.nextLine().split("\\W+"); int counter = 0; for (int i = 0; i < input.length; i++) { counter++; } System.out.println(counter); } }