题解 | #表示数字#

表示数字

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 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            StringBuilder sb =new StringBuilder();
            // flag true代表上一个是数字 否则不是数字
            boolean flag=false;
            for(int i=0;i<s.length();i++){
                char ch=s.charAt(i);
                if(ch>='0'&&ch<='9'){
                    if(!flag){
                        sb.append("*");
                        sb.append(ch);
                        flag=true;
                    }else{
                        sb.append(ch);
                    }
                    if(i==s.length()-1){
                        sb.append("*");
                    }
                }else{
                    if(flag){
                        sb.append("*");
                        sb.append(ch);
                        flag=false;
                    }else{
                        sb.append(ch);
                    }
                }
            }
            System.out.println(sb);

        }
    }
}

全部评论

相关推荐

今天 14:14
门头沟学院 Java
7.10投递7.15感谢信
投递地平线等公司9个岗位
点赞 评论 收藏
分享
牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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