return new int[]{randomHeight, randomWidth};

return new int[]{randomHeight, randomWidth};

我想为我们在大学玩的扫雷小游戏编写一些基本内容。现在我的代码 .

出现问题
public class Minesweeper1 {

    public static int[][]  makeRandomBoard(int s, int z, int n){
        //creating the field and fill with 0
        int feld[][] = new int [s][z];
        for(int i = 0; i < s; i++){
            for(int j = 0; j < z; j++){
                feld[i][j] = 0;
            }
        }

        //n-times to fill the field
        for( int i = 0; i < n; i++){
            selectRandomPosition(s, z);
            //want to get them from selectRandomPosition
            feld[randomHeight][randomWidth] = 1;
        }
    }
}

因此它启动 selectRandomPosition 代码:

public static int[] selectRandomPosition(int maxWidth, int maxHeight) {
    int randomHeight = StdRandom.uniform(0, maxHeight);
    int randomWidth = StdRandom.uniform(0, maxWidth);
    return new int[]{randomHeight, randomWidth};
}

这里我不允许更改任何内容,但它 returns 是一个新数组。现在我的问题是如何在 makeRandomBoard 方法中使用新数组,因为我不知道数组的任何名称。当我使用 feld[randomHeight][randomWidth] = 1; 时,它说它不知道这些变量。

how I can use the new array in my makeRandomBoard method, since I do not know any name of the array?

调用该方法,并将其 return 值赋给一个变量。现在您有了数组的名称:

// Make a call
int[] randomArray = selectRandomPosition(maxW, maxH);
// Access the width
int randomW = randomArray[0];
// Access the height
int randomH = randomArray[1];