题解 | #表示数字#

表示数字

https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6

import java.util.*;
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String s = br.readLine();
            StringBuffer stb = new StringBuffer();
            int count = 0;
            for (int i = 0; i < s.length(); i++) {
                if (!Character.isDigit(s.charAt(i))) {
                    if (count != 0) {
                        stb.append("*").append(s.charAt(i));
                        count = 0;
                    } else {
                        stb.append(s.charAt(i));
                    }
                } else {
                    count++;
                    if (count == 1) {
                        stb.append("*").append(s.charAt(i));
                    } else {
                        stb.append(s.charAt(i));
                    }
                }
            }
             if(Character.isDigit(s.charAt(s.length()-1))){
                System.out.println(stb.append("*").toString());
            }else {
                System.out.println(stb.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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