题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
1、判断是否是字符串内内容,可以根据flag去做一个判断,如果是则设置为true,在为true期间,遇到空格不需要把数据写进去。
2、在flag为false期间,字符为空格时,把数据写进list,并清空字符串。
3、正常情况把数据写入,遇到字符串时,把flag标记为true。
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { private static boolean flag = false; public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String data = in.nextLine(); data+=" "; List<String> res = new ArrayList<>(); StringBuilder sb = new StringBuilder(); for (char c : data.toCharArray()) { if (!flag && c == ' ') { res.add(sb.toString()); sb = new StringBuilder(); } else { if (c == '"') { if (!flag) { flag = true; } else { flag = false; } } else { sb.append(c); } } } System.out.println(res.size()); for (String str:res) { System.out.println(str); } } } }