解题思路:
首先考虑第一个条件“前N行字符原样输出”,需要使用到输入的nextline()函数,然后输出。再考虑第二个条件需要分割,回车可以用前面的nextline()函数 ,空格分割需要用到字符串的分割函数(String类型)+"."split(" ")。 注意事项:输入的N的回车会阻塞nextline的输入(因为nextLine()读取的是nextInt()的回车换行符,导致无法输入)
参考代码:
import java.util.Scanner;
public class Main {
public static void main(String[]argvs) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++)
{
String s=sc.nextLine();
System.out.println(s);
System.out.println();
}
while(sc.hasNextLine())
{
String temp=sc.nextLine();
String[] s=temp.split(" ");
for(int i=0;i<s.length;i++) {
System.out.println(s[i]+" ");
System.out.println();
}
}
}
}
0.0分
2 人评分