题解 | #参数解析#

参数解析

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()+" ";//末尾加上空格
            int start=0,end=1;
            List<String> list = new ArrayList<>();
            while(end<=line.length()) {
                if(line.charAt(start) == '\"') {
                    start++;
                    end=start+1;
                    while(line.charAt(end)!='\"') {
                        end++;
                    }
                    list.add(line.substring(start, end));
                    start = end+2;
                } else {
                    while(line.charAt(end)!=' ') {
                        end++;
                    }
                    list.add(line.substring(start, end));
                    start = end+1;
                }
                end = start+1;
            }
            System.out.println(list.size());
            for(String s:list) {
                System.out.println(s);
            }
        }
    }
}

全部评论

相关推荐

面了100年面试不知...:今年白菜这么多,冬天可以狂吃了
点赞 评论 收藏
分享
2025-12-05 18:09
已编辑
广东药科大学 后端工程师
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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