题解 | #参数解析#

参数解析

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

import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
// 注意类名必须为 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 s = in.nextLine();
            List<String> list = getParameters(s);
            System.out.println(list.size());
		    for(String str:list){
			   System.out.println(str);
		    }
        }
    }
    private static List<String> getParameters(String s) {
        List<String> list = new ArrayList<>();
        String[] str = s.split(" ");
        for (int i = 0; i < str.length; i++) {
            if (!str[i].contains("\"")) {
                list.add(str[i]);
            } else if (str[i].charAt(str[i].length() - 1) == '\"') {
                list.add(str[i].replace("\"", ""));
            } else {
                String temp = str[i];
                while (!str[i + 1].contains("\"")) {
                    i++;
                    temp += " " + str[i];
                }
                i++;
                temp += " " + str[i];
                list.add(temp.replace("\"", ""));
            }
        }
        return list;

    }
}

全部评论

相关推荐

03-31 18:02
门头沟学院 Java
白日梦想家_等打包版:不要的哦佛给我
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务