题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String s = in.nextLine();
List<String> list = getParameters(s);
System.out.println(list.size());
for(String str:list){
System.out.println(str);
}
}
}
private static List<String> getParameters(String s) {
List<String> list = new ArrayList<>();
String[] str = s.split(" ");
for (int i = 0; i < str.length; i++) {
if (!str[i].contains("\"")) {
list.add(str[i]);
} else if (str[i].charAt(str[i].length() - 1) == '\"') {
list.add(str[i].replace("\"", ""));
} else {
String temp = str[i];
while (!str[i + 1].contains("\"")) {
i++;
temp += " " + str[i];
}
i++;
temp += " " + str[i];
list.add(temp.replace("\"", ""));
}
}
return list;
}
}
腾讯公司福利 1143人发布
查看14道真题和解析
