使用3个数学运算符的每种可能组合填充2D数组

时间:2016-10-27 01:32:24

标签: java arrays logic

我想构建一个可以容纳四个数学运算符的每3项组合的二维数组(+ - * /) 该数组应该看起来像这样
[[, - , - ]
[[,*,*]
[[,,/,/]

...
[[,*,*]
[[/,/,/]
但是我遇到了一些问题。我的代码在打印组合时,每个组合打印3次。其次,我不太确定如何让每个操作员进入阵列中的位置。
这是我的代码

String[][] allOps = new String[27][3];
    int count = 1;

    String[] ops = new String[] {"+","-","*","/"};
    for (int x=1;x<4;x++) 
    { 
        for (int y=1;y<4;y++)  
        {
            for (int z=1;z<4;z++)
            {


                //System.out.println(ops[x] + " " + ops[y] + " " + ops[z] + " " + count);
                //count++;
            }
        }
    }

有没有办法让每个操作员使用for循环来获取它的特定位置([*] [*] [*])?

3 个答案:

答案 0 :(得分:0)

您有一个小错误,将您的上一个操作[y]更改为操作[z]并将ur x = 1更改为x = 0,y = 1更改为y = 0,z = 1更改为z = 0,这是因为阵列从0-3开始而不是1-3,因此现在你可以看到带有“+”的组合,如果你觉得有帮助就标记为已答案。

String[][] allOps = new String[27][3];
int count = 1;

String[] ops = new String[] {"+","-","*","/"};
for (int x=0;x<4;x++) 
{ 
    for (int y=0;y<4;y++)  
    {
        for (int z=0;z<4;z++)
        {


            System.out.println(ops[x] + " " + ops[y] + " " + ops[z] + " " + count);
            count++;
        }
    }
}

答案 1 :(得分:0)

试试这个,您需要使用运算符构造新数组并将其添加到从零开始的索引中,您将获得的最大组合是64

    String[][] allOps = new String[64][3];
    int count = 0;
    for (int x = 0; x < 4; x++) {
        for (int y = 0; y < 4; y++) {
            for (int z = 0; z < 4; z++) {
                allOps[count][0] = ops[x];
                allOps[count][1] = ops[y];
                allOps[count][2] = ops[z];
                count++;
            }
        }
    }
    System.out.println(count);
    System.out.println(Arrays.deepToString(allOps));

使用java8流

    String[] ops = new String[] { "+", "-", "*", "/" };
    List<String> opsList = Arrays.asList(ops);
    String[][] combinations = opsList.stream()
                                .flatMap(x -> opsList.stream().flatMap(y -> opsList.stream().map(z -> new String[] { x, y, z })))
                                .toArray(String[][]::new);
    System.out.println(Arrays.deepToString(combinations));

组合

[[+, +, +], [+, +, -], [+, +, *], [+, +, /], [+, -, +], [+, -, -], [+, -, *], [+, -, /], [+, *, +], [+, *, -], [+, *, *], [+, *, /], [+, /, +], [+, /, -], [+, /, *], [+, /, /], [-, +, +], [-, +, -], [-, +, *], [-, +, /], [-, -, +], [-, -, -], [-, -, *], [-, -, /], [-, *, +], [-, *, -], [-, *, *], [-, *, /], [-, /, +], [-, /, -], [-, /, *], [-, /, /], [*, +, +], [*, +, -], [*, +, *], [*, +, /], [*, -, +], [*, -, -], [*, -, *], [*, -, /], [*, *, +], [*, *, -], [*, *, *], [*, *, /], [*, /, +], [*, /, -], [*, /, *], [*, /, /], [/, +, +], [/, +, -], [/, +, *], [/, +, /], [/, -, +], [/, -, -], [/, -, *], [/, -, /], [/, *, +], [/, *, -], [/, *, *], [/, *, /], [/, /, +], [/, /, -], [/, /, *], [/, /, /]]

答案 2 :(得分:0)

将四个运算符*, - ,/和+排列为3个位置的64(4 * 4 * 4)种组合。你需要在这里改变你的代码,你的for循环从索引0开始,然后继续循环。这里怎么样,虽然不确定为什么要2D阵列

String[][] arrayOfCombination = new String[64][1];
    int count = 0;
    String[] ops = new String[] { "+", "-", "*", "/" };
    for (int x = 0; x < 4; x++) {
        for (int y = 0; y < 4; y++) {
            for (int z = 0; z < 4; z++) {

                arrayOfCombination[count][0] = ops[x] + " " + ops[y] + " " + ops[z];
                count++;
            }
        }
    }
    for (int row = 0; row < 64; row++) {

        System.out.println(arrayOfCombination[row][0]);

    }

更好的方法是使用ArrayList,如下所示,

ArrayList<String> list = new ArrayList();

//adding combinations
list.add("[" + ops[x] + " " + ops[y] + " " + ops[z] + "]");

//printing them
System.out.println(list);