javaTest
表示数字
http://www.nowcoder.com/questionTerminal/637062df51674de8ba464e792d1a0ac6
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
@Author: zhouLai
@Date: 2020/3/23 20:05
@Version 1.0
/
public class Main {public static void main(String[] args) throws Exception{/*将一个字符中所有出现的数字前后加上符号“*”,其他字符保持不变 public static String MarkNum(String pInStr) { return null; } 注意:输入数据可能有多行*/ /* Scanner sc = new Scanner(System.in); char[] chars = sc.nextLine().toCharArray();*/ int zero = Character.valueOf('0').hashCode(); int nine = Character.valueOf('9').hashCode(); ArrayList<Character> target = new ArrayList<>(); for (int i = zero; i <= nine; i++) { target.add((char)i); } Scanner sc = new Scanner(System.in); while (sc.hasNext()){ String s = sc.nextLine(); StringBuilder sb = new StringBuilder(); char[] chars = s.toCharArray(); for (int i = 0; i < chars.length;) { if (target.contains(chars[i])){ int count = 0; out:for (int j = 0; j < chars.length - i;j++) { if (target.contains(chars[i+j])){ count++; }else { break out; } } if (count>1){ for (int k = 0; k < count; k++) { if (k==0){ sb.append("*"+chars[i+k]); }else if (k==count-1){ sb.append(chars[i+k]+"*"); }else { sb.append(chars[i+k]); } } i = i+count; }else { sb.append("*"+chars[i]+"*"); i++; } }else { sb.append(chars[i]); i++; } } System.out.println(sb.toString()); }
}
}