题解 | 参数解析

参数解析

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s1 = in.nextLine();
        String s[] = s1.split("\\s+");
        int flag = 0;
        int x = 0;
        int sum = 1;
        for(int i = 0; i < s1.length(); i++){
            if(s1.charAt(i) == '\"'){
                if(x == 0){
                    x = 1;
                    continue;
                }
                if(x == 1){
                    x = 0;
                    continue;
                }
            }
            if(s1.charAt(i) == ' '){
                if(x == 0){
                    sum++;
                }
            }
        }
        System.out.println(sum);
        for(int i = 0; i < s.length; i++){
            if(s[i].charAt(0) == '"'){
                flag = 1;
                if(s[i].lastIndexOf('\"') >0){
                    flag = 0;
                }
                s[i] = s[i].replaceAll("\"","");
                System.out.print(s[i]);
                if(flag == 0){
                    System.out.println();
                }
                continue;
            }
            if(s[i].lastIndexOf('\"') >0){
                flag = 0;
                System.out.print(" "+ s[i].replaceAll("\"",""));
                System.out.println();
                continue;
            }
            if(!s[i].contains("\"")){
                if(flag == 1){
                    System.out.print(" "+s[i]);
                }else{
                    System.out.print(s[i]);
                    System.out.println();
                }
            }
        }
        
    }
}

全部评论

相关推荐

阿武同学:基本信息保留前面三行,其他的可以全部删掉,邮箱最重要的你没写,主修课程精简到8个以内,实习里面2/3/4都是水内容的,非要写的话建议两到三句话,项目经历排版优化下,自我评价缩到三行
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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