题解 | #表示数字#

表示数字

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        String res = "";
        int end = 0;
        int start = 0;
        while (end < str.length()) {
            while (end < str.length() && !Character.isDigit(str.charAt(end))) {
                end++;
            }
            String tmp = str.substring(start, end);
            // System.out.println("tmp:" + tmp);
            res += tmp;
            start = end;
            while (end < str.length() && Character.isDigit(str.charAt(end))) {
                end++;
            }
            if (end > start) {
                String digitStr = "*" +str.substring(start, end) + "*";
                res += digitStr;
            }
            start = end;
        }
        System.out.println(res);
    }
}

全部评论

相关推荐

哇哇的菜鸡oc:他这不叫校招offer,而是实习offer
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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