题解 | #表示数字#

表示数字

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

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str=in.nextLine();
            StringBuilder sb=new StringBuilder();
            int i=0;
            while(i<str.length()){
                if(str.charAt(i)>='0' && str.charAt(i)<='9'){
                    sb.append("*");//数字的头部加*
                    while(i<str.length() && str.charAt(i)>='0' && str.charAt(i)<='9'){
                        sb.append(str.charAt(i));
                        i++;
                    }
                    sb.append("*");//数字尾部加*
                }else{//非数字,直接添加到字符串中
                    sb.append(str.charAt(i));
                    i++;
                }
            }
            System.out.println(sb.toString());
        }
    }
全部评论

相关推荐

01-12 20:31
东北大学 Java
点赞 评论 收藏
分享
兑生:一开始只是想找个实习,找到实习了想进大厂,进大厂了想转正,转正了想offer打牌,打牌了想要大包,大包了想要wlb……
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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