【问题标题】:Getting 2d array from a method where I use switch从我使用 switch 的方法中获取二维数组
【发布时间】:2020-06-15 16:41:05
【问题描述】:

您好,我想从使用 switch 语句的方法中获取二维数组。我想取出num1或num2,所以其他方法可以使用这个数字。这可能吗,怎么做? 这是我的代码:

public int[][] gameNumbers(){

    int rand = (int)(Math.random() * 2) + 1;

    switch(rand) {
    case 1:
        int [][] num1= {{4,3,5,2,6,9,7,8,1},{6,8,2,5,7,1,4,9,3},{1,9,7,8,3,4,5,6,2},{8,2,6,1,9,5,3,4,7},{3,7,4,6,8,2,9,1,5},{9,5,1,7,4,3,6,2,8},{5,1,9,3,2,6,8,7,4},{2,4,8,9,5,7,1,3,6},{7,6,3,4,1,8,2,5,9}};
        break;
    case 2:
        int [][] num2= {{1,7,4,3,5,2,9,6,8},{5,3,6,8,7,1,6,4,5,3},{2,9,8,7,1,6,4,5,3},{4,2,3,1,7,8,6,9,5},{8,5,7,2,6,9,3,4,1},{9,6,1,4,3,5,7,8,2},{3,8,2,6,4,7,5,1,9},{7,4,9,5,2,1,8,3,6},{6,1,5,9,8,3,2,7,4}};
        break;
    default:
        break;
    }

    return rand;
}

【问题讨论】:

  • 数组可以是随机的吗?如果是这样,为什么不直接生成一个随机数组而不是 switch
  • 另外,如果您在default 的情况下不做任何事情,您就不需要编写它。而且你的方法是错误的,它返回了一个无效类型,因为在签名中你让它返回int[][],而你试图简单地返回int
  • @chriptus13 数组不能是随机的,那些是数独的数组:)

标签: java arrays switch-statement return-value


【解决方案1】:

由于您想获得可能的十个 9x9 阵列之一,您可以这样尝试。您将这十个数组放在另一个数组中,然后随机返回一个。

private static int[][][] arrays = new int[][][]{
        {{4, 3, 5, 2, 6, 9, 7, 8, 1}, {6, 8, 2, 5, 7, 1, 4, 9, 3}, {1, 9, 7, 8, 3, 4, 5, 6, 2}, {8, 2, 6, 1, 9, 5, 3, 4, 7}, {3, 7, 4, 6, 8, 2, 9, 1, 5}, {9, 5, 1, 7, 4, 3, 6, 2, 8}, {5, 1, 9, 3, 2, 6, 8, 7, 4}, {2, 4, 8, 9, 5, 7, 1, 3, 6}, {7, 6, 3, 4, 1, 8, 2, 5, 9}},
        {{1, 7, 4, 3, 5, 2, 9, 6, 8}, {5, 3, 6, 8, 7, 1, 6, 4, 5, 3}, {2, 9, 8, 7, 1, 6, 4, 5, 3}, {4, 2, 3, 1, 7, 8, 6, 9, 5}, {8, 5, 7, 2, 6, 9, 3, 4, 1}, {9, 6, 1, 4, 3, 5, 7, 8, 2}, {3, 8, 2, 6, 4, 7, 5, 1, 9}, {7, 4, 9, 5, 2, 1, 8, 3, 6}, {6, 1, 5, 9, 8, 3, 2, 7, 4}}
        // ... more 8 arrays
};

public int[][] gameNumbers() {
    int idx = (int) (Math.random() * 10);
    return arrays[idx];
}

【讨论】:

  • 不,我想要的是从 switch case 中的数组中选择一个数组。这就是问题所在,我不知道如何让它们脱离开关。
  • 检查第二种方法。
  • 是的,我考虑过使用 if 语句,但我想要十个二维数组,所以我应该只使用 if 和 else if 语句吗?比如 if(math.rand() == 1), else if(math.rand() == 2)...?
  • 哦,在这种情况下使用switch
  • 您甚至可以将这十个数组放入一个数组中,并使用随机结果对其进行索引。这样你就可以把它作为一行。
【解决方案2】:

你已经很接近了。应该这样做。

在switch语句前声明result变量,并在switch内部赋值。

public int[][] gameNumbers(){

    // Declare this before the switch so it stays in scope.
    int[][] result;

    int rand = (int)(Math.random() * 2) + 1;
    switch(rand) {
    case 1:
        result = new int[][]{{4,3,5,2,6,9,7,8,1},{6,8,2,5,7,1,4,9,3},{1,9,7,8,3,4,5,6,2},{8,2,6,1,9,5,3,4,7},{3,7,4,6,8,2,9,1,5},{9,5,1,7,4,3,6,2,8},{5,1,9,3,2,6,8,7,4},{2,4,8,9,5,7,1,3,6},{7,6,3,4,1,8,2,5,9}};
        break;

    case 2:
        result = new int[][]{{1,7,4,3,5,2,9,6,8},{5,3,6,8,7,1,6,4,5,3},{2,9,8,7,1,6,4,5,3},{4,2,3,1,7,8,6,9,5},{8,5,7,2,6,9,3,4,1},{9,6,1,4,3,5,7,8,2},{3,8,2,6,4,7,5,1,9},{7,4,9,5,2,1,8,3,6},{6,1,5,9,8,3,2,7,4}};
        break;

    default:
        result = null;
        break;
    }
    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多