题解 | #表示数字#

表示数字

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());
        }
    }
全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 11:00
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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