题解 | #表示数字#

表示数字

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);
        String s=in.next();
        boolean start=false;
        StringBuffer res=new StringBuffer();
        for(int i=0;i<s.length();i++){
            char c=s.charAt(i);
            if(Character.isDigit(c)&&start==false){
                res.append("*"+c);  //1. 遍历到数字串开头且未开始加星号,就加星号,标记符号表示开始添加符号。
                start=true;
            }else if(!Character.isDigit(c)&&start){
                res.append("*"+c);  //2. 遍历到数字串且已开始加星号,就末尾加星号,标记星号已完成。
                start=false;
            }else{
                res.append(c); // 3.非首尾数字和非数字字符直接添加。
            }
        }
        // 注意末尾字符是数字时没加尾星号,所以再判断一下
        if(Character.isDigit(res.charAt(res.length()-1)))res.append("*");
        System.out.print(res.toString());
    }
}

全部评论

相关推荐

求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
05-22 17:07
已编辑
门头沟学院 Java
程序员牛肉:都啥时候了还jb打蓝桥杯呢,有限找实习。
点赞 评论 收藏
分享
这算盘打的
程序员小白条:都这样的,都是潜规则,你自己说可以实习一年就行了,实习可以随便跑路的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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