题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
import java.util.*; /** 暴力解法,全部遍历 */ 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(); char[] arr = s.toCharArray(); int count = 0; boolean flag = false; //引号标记 int i = 0; int j = 0; List<String> list = new ArrayList<>(); while (j < arr.length) { if (arr[j] == '"') { flag = !flag; } if (arr[j] == ' ' && flag == false) { count++; if (arr[i] == '"') { list.add(s.substring(i + 1, j - 1)); } else { list.add(s.substring(i, j)); } i = j + 1; } j++; } count++; if (arr[i] == '"') { list.add(s.substring(i + 1, j - 1)); } else { list.add(s.substring(i, j)); } System.out.println(count); for (String str : list) { System.out.println(str); } } } }