当前位置:编程学习 > JAVA >>

已知一个残缺不全的手机号码 怎么用java把所有符合条件的号码输出?

已知手机号码为:15X10XX115X

我自己用数组做的 但是不知道怎么将所有情况都显示出来 总是输出不全所有情况

求大神分析!!!!! --------------------编程问答-------------------- for --------------------编程问答-------------------- 排列,穷举法

--------------------编程问答-------------------- 就是用一下循环就行了呀 --------------------编程问答--------------------
引用 2 楼 awusoft 的回复:
排列,穷举法

正解, --------------------编程问答-------------------- 无聊写了下,

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);
}

}

补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,