2010-09-06 96 views
1

我想製作一個程序,它需要一組數字(如234),並且此時可以打印手機鍵盤上可能存在的每個字母組合。用於文本的遞歸編程預測文本

(1 - 什麼都沒有,2 - ABC,3高清等)

我目前有:

import java.util.*; 

public class testCombo { 
    static String str="217"; 
    static ArrayList<String> list=new ArrayList<String>(); 

    static void addLet(String seg){ 
     if(seg.length()==str.length()){ 
      list.add(seg); 
      return; 
     } 
     char currentChar=str.charAt(seg.length()); 
     if(currentChar==1 || currentChar==0) 
     { 
      String str1=seg+" "; 
      addLet(str1); 
     } 
     if(currentChar=='2'){ 
      addLet(seg+"a"); 
      addLet(seg+"b"); 
      addLet(seg+"c"); 
     } 
     else if(currentChar=='3'){ 
      addLet(seg+"d"); 
      addLet(seg+"e"); 
      addLet(seg+"f"); 
     } 
     else if(currentChar=='4'){ 
      addLet(seg+"g"); 
      addLet(seg+"h"); 
      addLet(seg+"i"); 
     } 
     else if(currentChar=='5'){ 
      addLet(seg+"j"); 
      addLet(seg+"k"); 
      addLet(seg+"l"); 
     } 
     else if(currentChar=='6'){ 
      addLet(seg+"m"); 
      addLet(seg+"n"); 
      addLet(seg+"o"); 
     } 
     else if(currentChar=='7'){ 
      addLet(seg+"p"); 
      addLet(seg+"q"); 
      addLet(seg+"r"); 
      addLet(seg+"s"); 
     } 
     else if(currentChar=='8'){ 
      addLet(seg+"t"); 
      addLet(seg+"u"); 
      addLet(seg+"v"); 
     } 
     else if(currentChar=='9'){ 
      addLet(seg+"w"); 
      addLet(seg+"x"); 
      addLet(seg+"y"); 
      addLet(seg+"z"); 
     } 
    } 

    public static void main(String[] args){ 
     addLet(""); 
     for(String str:list) //Sets str to each value in list during each iteration 
      System.out.println(str); 
    } 
} 

我的代碼,我們應該使用遞歸編程,但我不能讓它爲1s和0s工作。 (這只是一個練習課,我有另一個允許用戶輸入,它已經驗證它只包含數字)

這種方式發現然後打印出每個組合計數爲遞歸?

+0

正在做作業嗎? – bramp 2010-09-06 09:57:25

+0

爲什麼你爲每個字符串聲明一個新的變量,然後你只是將它用作'addLet'的參數?爲什麼不使用'addLet(seg +「x」)'等? – 2010-09-06 09:59:24

+0

嗯是啊,我沒有考慮這樣做,這樣做很有意義;/ – Admiration 2010-09-06 10:07:07

回答

1

是的,它是遞歸的(它通過調用自己的方式工作),但它不必要的冗長。您可以跳過臨時變量,從而節省大量空間,並使其更具可讀性。我花了一些時間來神交爲什麼你不得不在各種情況下的幾個字符串變量:

if(currentChar==1 || currentChar==0) 
    { 
     addLet(seg+" "); 
    } 
    if(currentChar=='2'){ 
     addLet(seg+"a"); 
     addLet(seg+"b"); 
     addLet(seg+"c"); 
    } ... 

WRT 1和0,你應該比較currentChar到'1''0',不10

0

可以簡化碼並通過數字映射候選字母減少出錯的機會:

import java.util.*; 
import static java.util.Arrays.asList; 

public class TestCombo { 

    private static Map<Character, List<Character>> lettersByDigit = 
     new HashMap<Character, List<Character>>() {{ 
      put('0', asList(' ')); 
      put('1', asList(' ')); 
      put('2', asList('a', 'b', 'c')); 
      put('3', asList('d', 'e', 'f')); 

      // and so on - add all candidates per digit in this fashion 
     }}; 

    private static List<String> candidates = new Vector<String>(); 

    private static void enumerate(String digits, String prefix) { 
     if (prefix.length() == digits.length()) { 
      candidates.add(prefix); 
      return; 
     } 

     char nextDigit = digits.charAt(prefix.length()); 

     for (Character nextLetter : lettersByDigit.get(nextDigit)) { 
      enumerate(digits, prefix + nextLetter.toString()); 
     } 
    } 

    public static void main(String[] args){ 
     enumerate("217", ""); 
     for(String candidate : candidates) { 
      System.out.println(candidate); 
     } 
    } 
} 

請注意,這不是測試,但希望你的想法。