精选问答
java编程题,输入一段英文文章,单词之间都已经用空格分隔,本人想以每5个单词为一行输出,怎么写?请指教我的问题是不会输出5个单词为一行.求指导!我的代码是以下:import java.io.* ;import java.util.* ;public class Test2{ public static void main(String[] args)throws IOException { InputStreamReader reader = new InputStreamReader(System.i

2019-06-18

java编程题,输入一段英文文章,单词之间都已经用空格分隔,本人想以每5个单词为一行输出,怎么写?请指教
我的问题是不会输出5个单词为一行.求指导!我的代码是以下:
import java.io.* ;
import java.util.* ;
public class Test2
{ public static void main(String[] args)throws IOException
{ InputStreamReader reader = new InputStreamReader(System.in) ;
BufferedReader input = new BufferedReader(reader) ;
String a ;
System.out.println("请输入一段英文文章:") ;
a = input.readLine() ;
for( int i = 0 ; i < a.length() ; i++ ) //将文章中的标点符号变换为空格
{
if( a.charAt(i) >= 'a' && a.charAt(i) = 'A' && a.charAt(i)
优质解答
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
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
相关问答