题解 | #参数解析#
参数解析
https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
// 1. j计算分解后的参数个数
int count = 0;//计算除双引号以外的空格数量,最终空格数加1,就是要分解的参数的个数
for (int i = 0; i < str.length(); i++) {
//通过do while 来把双引号中的元素走完
if (str.charAt(i) == '"'){
do {
i++;
}while (str.charAt(i) !='"');
}
//当没有双引号,出现空格就加一次
if (str.charAt(i) == ' '){
count ++;
}
}
//最后在空格数上在加一就是参数的个数
count ++;
System.out.println(count);
// 2. 遍历分解后的参数
int fig = -1;//用来判断 双引号
for (int i = 0; i < str.length(); i++) {
//判断 该参数是否在双引号中
if (str.charAt(i) == '"'){
fig *= -1;
}
// 除了双引号中的空格和 " ,其他字都打印
if (str.charAt(i) != ' ' && str.charAt(i) != '"'){
System.out.print(str.charAt(i));
}
// 打印 双引号中的 空格
if (fig > 0 && str.charAt(i) == ' '){
System.out.print(' ');
}
// 出现 除双引号以外的空格就换行
if (fig < 0 && str.charAt(i) == ' '){
System.out.println();
}
}
}
}
叮咚买菜公司氛围 118人发布