将一个字符中所有出现的数字前后加上符号“*”,其他字符保持不变
示例1:
Jkdi234klowe90a3
Jkdi*234*klowe*90*a*3*
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ String s = scanner.nextLine(); getCubeRoot(s); } } public static void getCubeRoot(String str) { char[] chars = str.toCharArray(); StringBuilder builder = new StringBuilder(); boolean isFirst = true; int len = chars.length - 1; for (int i = 0; i < chars.length; i++) { if(chars[i] >= '0' && chars[i] <= '9'){ if(isFirst){ builder.append("*").append(chars[i]); }else { builder.append(chars[i]); } if(len == i){ builder.append("*"); } isFirst = false; }else { if(!isFirst){ builder.append("*"); } isFirst = true; builder.append(chars[i]); } } System.out.println(builder); } }