java编程题从键盘输入一段英文,统计这段文字中单词的个数,并输出其中由四个字母组成的单词以5个一行输
展开全部
import java.util.Scanner;
public class SplitWords {
public static void main(String[] args) {
System.out.println("Please input some english words, separate with ',' or SPACE ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] words = input.replace(',', ' ').replace('.', ' ').split("\\s+");
System.out.println("Total " + words.length + " separate word in " + input);
System.out.println("Words with 4 characters are as follows: ");
int count = 0;
for(String str: words){
if(count == 5){
System.out.println();
count = 0;
}
if(str.length() == 4){
System.out.print(str + "\t");
count++;
}
}
}
}
------------------
Please input some english words, separate with ',' or SPACE
Today is Christmas Day, we people in china don't celebrate it. I like china very much.
Total 16 separate word in Today is Christmas Day, we people in china don't celebrate it. I like china very much.
Words with 4 characters are as follows:
like very much
public class SplitWords {
public static void main(String[] args) {
System.out.println("Please input some english words, separate with ',' or SPACE ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
String[] words = input.replace(',', ' ').replace('.', ' ').split("\\s+");
System.out.println("Total " + words.length + " separate word in " + input);
System.out.println("Words with 4 characters are as follows: ");
int count = 0;
for(String str: words){
if(count == 5){
System.out.println();
count = 0;
}
if(str.length() == 4){
System.out.print(str + "\t");
count++;
}
}
}
}
------------------
Please input some english words, separate with ',' or SPACE
Today is Christmas Day, we people in china don't celebrate it. I like china very much.
Total 16 separate word in Today is Christmas Day, we people in china don't celebrate it. I like china very much.
Words with 4 characters are as follows:
like very much
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询