問題 1094: 字符串的輸入輸出處理
題目描述
字符串的輸入輸出處理。
輸入
第一行是一個正整數(shù)N,最大為100。之后是多行字符串(行數(shù)大于N), 每一行字符串可能含有空格,字符數(shù)不超過1000。
輸出
先將輸入中的前N行字符串(可能含有空格)原樣輸出,再將余下的字符串(不含有空格)以空格或回車分割依次按行輸出。每行輸出之間輸出一個空行。
樣例輸入
2
www.dotcpp.com DOTCPP
A C M
D O T CPP
樣例輸出
www.dotcpp.com DOTCPP
A C M
D
O
T
CPP
方法一:
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* User: 76147
* Date: 2020-01-27
* Time: 13:58
* Description:
*/
public class 字符串的輸入輸出處理 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
for (int i = 0; i < n; i++) {
String str = sc.nextLine();
System.out.println(str + "\n");
}
while (sc.hasNext()) {
String str = sc.next();
System.out.println(str + "\n");
}
}
}
方法二:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int i = 0;
int k = 1;
String str[] = new String[1000];
while (scanner.hasNext()) {
for (; i < n + 1; i++) {
str[i] = scanner.nextLine();
}
str[i] = scanner.next();
i++;
while (k < i) {
System.out.println(str[k] + "\n");
k++;
}
}
}