1.使用正则表达式做,注意Java使用正则表达式,必须导入java.util.regex.Matcher和java.util.regex.Pattern包import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;public class test{public static void main(String[] args) { String str,temp; str = new Scanner(System.in).nextLine(); temp = Pattern.compile("[^0-9]").matcher(str).replaceAll(""); int result = Integer.parseInt(temp) - 123; System.out.println(result);}}2.使用字符串的substring方法做import java.util.Scanner;public class test{public static void main(String[] args) { String str,temp=""; str = new Scanner(System.in).nextLine(); for (int i = 0; i < str.length(); i++) { if ((int)str.substring(i, i+1).charAt(0)>=48 && (int)str.substring(i, i+1).charAt(0)<=57) { temp+=str.substring(i, i+1); } } int result = Integer.parseInt(temp) - 123; System.out.println(result);}}