题解 | #参数解析#

参数解析

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.hasNext()) { // 注意 while 处理多个 case
            String str = in.nextLine();
		  // 记录引号数量
            int c = 0;
            List<String> strs = new ArrayList<>();
            String s = "";
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) == '"') {
                    c++;
                    continue;
                }
                // 遇见空格,需要判断是否在引号中,如果不在引号中,则该命令结束,如果在引号中,则继续
                if (str.charAt(i) == ' ') {
                    if (c % 2 == 0) {
                        // 不是引号内的空格,则字符串结束
                        strs.add(s);
                        // 重置字符串
                        s = "";
                        continue;
                    } else {
					  // 是引号内的空格,不分割
                        s += str.charAt(i);
                    }
                } else {
                    s += str.charAt(i);
                }
            }
            // 处理最后一个字符
            strs.add(s);
            System.out.println(strs.size());
            for (String ans : strs) {
                System.out.println(ans);
            }
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
永不遗忘:才这么点算什么拉黑,我初筛连着挂几十次了,最后还是能进面
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务