【问题标题】:Change long if / else to short map logic将 long if / else 更改为 short map 逻辑
【发布时间】:2017-07-12 17:46:36
【问题描述】:

我创建了一个脚本,根据所选选项显示结果。

我用: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;

最终答案将是 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 个

A 数据的逻辑可能是什么结构,比如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
    }

【问题讨论】:

  • 我看到了四个二进制变量。使用一棵树。
  • @HypnicJerk 和@Compass 我可以缩短变量的数量;到 4 (A, B, C, D) 然后我得到每个元素都是 3 个值 -1; 0; 1
  • 也许 BitSet 作为字母的任意组合的表示将在这里有所帮助。然后您可以使用Map<BitSet, String> 来获取答案。
  • @Seelenvirtuose 它不会起作用,至少不是直接的方式。阅读问题:不完整的输入应该产生仍然可能的答案集。

标签: java android dictionary data-structures


【解决方案1】:

创建一个 POJO。布尔值仍然有效,因为您可以选择 null。 选择A时,A为真。选择B时,B是假的。没有选择是空的。

class Option {
    Boolean a;
    Boolean c;
    Boolean e;
    Boolean g;
    String value;

    //do some getters and setters

    boolean isValid(Boolean a, Boolean c, Boolean e, Boolean g) {
        if(this.a != a && a != null) {
            return false;
        }
        if(this.c != c && c != null) {
            return false;
        }
        if(this.e != e && e != null) {
            return false;
        }
        if(this.g != g && g != null) {
            return false;
        }

        return true;
    }
}

我们可以使用这个来填充我们的选项 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**

任何用 null 填充的值都将通过,任何未填充的值都将与其各自的值进行比较。

【讨论】:

    猜你喜欢
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 2011-03-24
    • 2012-05-17
    相关资源
    最近更新 更多