题解 | 参数解析

参数解析

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();
                }
            }
        }
        
    }
}

全部评论

相关推荐

迷茫的大四🐶:摊牌了,我是25届的,你们也不招我
点赞 评论 收藏
分享
Aurora23:属于挂一半,暂时进池子了,隔一段时间没有其他组捞的话就彻底结束了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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