题解 | #参数解析#

参数解析

https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677

import java.util.*;

// 注意类名必须为 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 line = in.nextLine();
            String[] commands = line.split(" ");
            boolean flag = false;
            String cmd = "";
            List<String> list = new LinkedList<String>();
            for (int i = 0; i < commands.length; i++) {
                if (commands[i].contains("\"") &&
                        commands[i].replaceAll("[^\"]","").length() == 1) { //包含引号的,打开开关开始统计
                    flag = !flag;
                }

                if (flag) {
                    cmd += commands[i] + " ";
                } else {
                    if (!"".equals(cmd)) {//去掉前后的引号 输出命令
                        list.add(cmd.substring(1) +
                                 commands[i].replace("\"", ""));
                        cmd = "";//恢复空串
                    } else {
                        list.add(commands[i].replace("\"", ""));
                    }
                }
            }
            System.out.println(list.size());
            for (String s : list) {
                System.out.println(s);
            }
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客企业服务