表示数字
表示数字
http://www.nowcoder.com/questionTerminal/637062df51674de8ba464e792d1a0ac6
StringBuilder 轻松解决
import java.util.*; public class Main { public Main() { } public String markNum(String pInStr) { StringBuilder res = new StringBuilder(); for (int i = 0; i < pInStr.length();) { // 若发现了数字 if (pInStr.charAt(i) >= '0' && pInStr.charAt(i) <= '9') { res.append('*'); while (i < pInStr.length() && pInStr.charAt(i) >= '0' && pInStr.charAt(i) <= '9') { res.append(pInStr.charAt(i++)); } res.append('*'); } else { res.append(pInStr.charAt(i++)); } } return res.toString(); } public static void main(String[] args) { Main solution = new Main(); Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String pInStr = in.nextLine(); String res = solution.markNum(pInStr); System.out.println(res); } } }