题解 | #参数解析#

参数解析

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);
            }
        }
    }
}

全部评论

相关推荐

07-09 18:28
门头沟学院 Java
写着提前批,结果还要实习4个月以上???
程序员牛肉:这种不用看,直接投了,面试的时候问对应的HR就行。有可能他们是直接复制的暑期实习的模板。
点赞 评论 收藏
分享
07-11 15:12
门头沟学院 Java
别人在上班,我就在工位上看看视频啥的,这正常吗?
程序员小白条:实习就是摸鱼,只是公司指标,把你进来了,可能那时候客户很多,但等你进来的时候,已经是淡季了,根本没多少需求,或者说根本不适合实习生去完成,因此你就每天干坐着就行,可能1,2个月都没需求
实习生的蛐蛐区
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务