题解 | #表示数字#

表示数字

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

相关推荐

2 1 评论
分享
牛客网
牛客企业服务