无聊写了下,import java.util.Scanner;/** * This class is used to change a telephone number such as * 150xx68xx02 to a legal telephone number and print them all. * @author zfz * */ public class PhoneNumber { //used to traversal char[] numArr = {'0','1','2','3','4','5','6','7','8','9'};
/** * The method to print the telephone number. * @param number: the telephone number that contains x */ public void printPhoneNumber(String number){ if(number.length() != 11){ System.out.println("The length is wrong!"); return; }
StringBuffer s = new StringBuffer(number);
printNum(s, 0); }
/** * The recursive function to print telephone number. * @param s: the telephone number * @param n: it means that the numbers before s[n] are all numbers now. * so we just need to deal the latter bits. */ private void printNum(StringBuffer s, int n){ if(n==11){ System.out.println(s); } else{ char tmp = s.charAt(n); if(tmp<'0' || tmp > '9'){ //now it's not a number and we should traversal it. for(char tmpChar : numArr){ s.setCharAt(n, tmpChar); printNum(s, n+1); s.setCharAt(n, 'x'); } } else{ printNum(s, n+1); } } } /** * @param args */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String telephoneNum = scanner.next(); new PhoneNumber().printPhoneNumber(telephoneNum); }}
* This class is used to change a telephone number such as
* 150xx68xx02 to a legal telephone number and print them all.
* @author zfz
*
*/
public class PhoneNumber {
//used to traversal
char[] numArr = {'0','1','2','3','4','5','6','7','8','9'};
/**
* The method to print the telephone number.
* @param number: the telephone number that contains x
*/
public void printPhoneNumber(String number){
if(number.length() != 11){
System.out.println("The length is wrong!");
return;
}
StringBuffer s = new StringBuffer(number);
printNum(s, 0);
}
/**
* The recursive function to print telephone number.
* @param s: the telephone number
* @param n: it means that the numbers before s[n] are all numbers now.
* so we just need to deal the latter bits.
*/
private void printNum(StringBuffer s, int n){
if(n==11){
System.out.println(s);
}
else{
char tmp = s.charAt(n);
if(tmp<'0' || tmp > '9'){
//now it's not a number and we should traversal it.
for(char tmpChar : numArr){
s.setCharAt(n, tmpChar);
printNum(s, n+1);
s.setCharAt(n, 'x');
}
}
else{
printNum(s, n+1);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String telephoneNum = scanner.next();
new PhoneNumber().printPhoneNumber(telephoneNum);
}}