代码 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int num1 = 0, num2 = 0; String A = in.next(); int da = in.nextInt(); String B = in.next(); int db = in.nextInt(); int startIndex = 0; while (startIndex != -1) { if ((startIndex = A.indexOf('0' + da, startIndex)) != -1) { num1 = 10 * num1 + da; startIndex++; } } startIndex = 0; while (startIndex != -1) { if ((startIndex = B.indexOf('0' + db, startIndex)) != -1) { num2 = 10 * num2 + db; startIndex++; } } System.out.println(num1 + num2); } } } 注意点 题目本身不难,但要注意超时问题,最开始思考利用字符串的toCharArray方法来遍历,可惜还是超时,后来利用了,indexOf方法,快速判断字符串中有多少个目标字符