Java编程题:输入手机号,输出手机号数字数组和索引数组
import java.util.Scanner; import java.util.Arrays; public class test{ public static void main(String[] args){ System.out.println("请输入手机号:"); Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String longStr = ""; for(char ch:s.toCharArray()){ if (Character.isDigit(ch)){ longStr += ch; } } String shortStr = ""; for(char ch:longStr.toCharArray()){ if (shortStr.indexOf(ch) == -1){ shortStr += ch; } } int[] arr = new int[shortStr.length()]; for(int i=0;i<shortStr.length();i++){ int one = Integer.parseInt(String.valueOf(shortStr.charAt(i))); arr[i] = one; } reverse(arr); int[] index = new int[longStr.length()]; for(int i=0;i<longStr.length();i++){ int one = Integer.parseInt(String.valueOf(longStr.charAt(i))); for(int j=0;j<arr.length;j++){ if (arr[j] == one){ index[i] = j; } } } System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(index)); String phoneStr = ""; for(int i:index){ phoneStr += arr[i]; } System.out.println(phoneStr); } private static void reverse(int[] arr){ for(int i=0;i<arr.length;i++){ for(int j=i+1;j<arr.length;j++){ if (arr[i]<arr[j]){ int val = arr[i]; arr[i] = arr[j]; arr[j] = val; } } } } }