题解 | #参数解析#

参数解析

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

 import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {

//  u "a e i o u" r


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        String[] array = str.split(" ");
        List<String> results = new LinkedList<>();
        StringBuilder stringBuilder = new StringBuilder();
        //是否等待后半个“
        boolean isWait = false;

        for (int i = 0; i < str.length(); i++) {
            char c = str.toCharArray()[i];
            if (c == '\"') {
                isWait = !isWait;
                if (i == str.length() - 1) {
                    results.add(stringBuilder.toString());
                }
            } else if (c == ' ') {
                if (isWait) {
                    stringBuilder.append(c);
                } else {
                    results.add(stringBuilder.toString());
                    stringBuilder.delete(0, stringBuilder.length());
                }
            } else {
                stringBuilder.append(c);
                if (i == str.length() - 1) {
                    results.add(stringBuilder.toString());
                }
            }
        }
        System.out.println(results.size());
        for (String s : results) {
            System.out.println(s);
        }
    }
} 

全部评论

相关推荐

07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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