2017-02-22 32 views
0

我創建了一個腳本,根據所選選項顯示結果。更改long if/else以縮短地圖邏輯

我使用:A,B,C,d,E,F,G,H(在未來將會更)

int A = 0; 
int B = 0; 
int C = 0; 
int D = 0; 
int E = 0; 
int F = 0; 
int G = 0; 
int H = 0; 

最終ansver將16組合中的一個。

A;C;E;G = Nr1 
A;C;E;H = Nr2 
A;C;F;G = Nr3 
A;C;F;H = Nr4 
A;D;E;G = Nr5 
A;D;E;H = Nr6 
A;D;F;G = Nr7 
A;D;F;H = Nr8 
B;C;E;G = Nr9 
B;C;E;H = Nr10 
B;C;F;G = Nr11 
B;C;F;H = Nr12 
B;D;E;G = Nr13 
B;D;E;H = Nr14 
B;D;F;G = Nr15 
B;D;F;H = Nr16 

我想顯示潛在的變體。
當你按下的組合:A, C' then answer is '"Nr1,Nr2,Nr3,Nr4"
當你按下的組合:A, G' then answer is '"Nr1,Nr3,Nr5,Nr7"
以後會更多的變數IJKL ....等,但答案將只有16

什麼可能是一個數據結構的邏輯,如Map,我有點卡住了?

重要 - 組合可以產生也和案件 例如:H;C;E;AE;C;A;H ....等答案是Nr1

If/Else似乎太長。 當前的代碼:

String scoreTeamA = "waiting for the results"; 


    if (A == 1) { 
     if (C == 1) { 
      if (E == 1) { 
       if (G == 1) { 
        scoreTeamA = "The answer is: Nr1"; //combination: A;C;E;G 
       } else if (H == 1) { 
        scoreTeamA = "The answer is: Nr2"; //combination: A;C;E;H 
       } else scoreTeamA = "Possible variants, one of: Nr1, Nr2"; //combination: A;C;E 

      } else if (F == 1) { 
       if (G == 1) { 
        scoreTeamA = "The answer is: Nr3"; //combination: A;C;F;G 
       } else if (H == 1) { 
        scoreTeamA = "The answer is: Nr4"; //combination: A;C;F;H 
       } else scoreTeamA = "Possible variants, one of: Nr3,Nr4"; //combination: A;C;F 

      } else scoreTeamA = "Possible variants, one of: Nr1,Nr2,Nr3,Nr4"; //combination: A;C; 

     } else if (D == 1) { 
      scoreTeamA = "Possible variants, one of: Nr5,Nr6,Nr7,Nr8";//combination: A;D; 
     } else 
      scoreTeamA = "Possible variants, one of: Nr1,Nr2,Nr3,Nr4,Nr5,Nr6,Nr7,Nr8"; //combination: A 
    } else if (B == 1) { 
     scoreTeamA = "Possible variants, one of: Nr9,Nr10,Nr11,Nr12,Nr13,Nr14,Nr15,Nr16"; //combination: B 
    } 
+0

我看到四個二進制變量。使用樹。 – Compass

+0

@HypnicJerk和@Compass我可以縮短變量的數量;到4('A,B,C,D'),那麼我得到這些元素的每一個是3值'-1; 0; 1' –

+0

也許一個['BitSet'](http://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html)作爲這些字母的任意組合的表示將有所幫助。然後你可以使用一個Map 來獲得答案。 – Seelenvirtuose

回答

0

創建一個POJO。布爾值仍然有效,因爲您可以選擇null作爲選項。 當選擇a時,a爲真。當選擇b時,b是錯誤的。沒有選擇是空的。

我們可以使用這個 https://stackoverflow.com/a/27008250/2958086

List<Option> option = new ArrayList<>(); 
final int n = 4; 
for (int i = 0; i < Math.pow(2, n); i++) { 
    String bin = Integer.toBinaryString(i); 
    while (bin.length() < n) 
     bin = "0" + bin; 
    char[] chars = bin.toCharArray(); 
    boolean[] boolArray = new boolean[n]; 
    for (int j = 0; j < chars.length; j++) { 
     boolArray[j] = chars[j] == '0' ? true : false; 
    } 
    list.add(new Option(boolArray, i+1)); //create this constructor 
} 

然後填充我們的選項,以搜索:

List<String> findValues(Boolean a, Boolean c, Boolean e, Boolean g) { 
    List<String> values = new ArrayList<>(); 

    for(Option option : options) { 
     if(option.isValid(a, c, e, g)) 
      values.add(option.getValue()); 
    } 
    return values;  
} 

要使用:

List<String> myValues = findValues(true, false, null, null); // selects all filings that are AD** 

任何VAL用null填充的ues將通過,任何不是的值都會與它們各自的值進行比較。