2016-11-20 5 views
1

私は大学でやっている小さな掃除機ゲームのための基本的なものをコーディングしたいと思っています。今私のコードに問題があります。戻り値新しい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}; 
} 

を私が何かを変更することができますが、それは新しい配列を返していませんよ。今私の質問makeRandomBoardメソッドで新しい配列を使用することができます、私は配列の名前を知らないので、私の質問です。 feld[randomHeight][randomWidth] = 1;を使用すると、これらの変数がわからないことがわかります。

+1

可能な複製をhttp://stackoverflow.com/questions/2378756/how-to-store-配列から返されるメソッドのJavaで) – BackSlash

+0

'randomHeight'と' randomWidth'は 'selectRandomPosition'関数のローカル変数です。この関数の外でこれらの変数を使用することはできません。 – McNets

+0

@mcNets私はそれを知っていますが、私の質問は、どの講義でもそれがなかったので、配列の名前がなくてもそこにどのようにそれらを置くのかということでした。 – FireCross

答えて

1

私はmakeRandomBoardメソッドでどのように新しい配列を使用できますか?配列の名前はわかりません。

メソッドを呼び出し、その戻り値を変数に代入します。今、あなたは、配列の名前があります([Javaではメソッドによって返された配列を格納する方法]の

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

ありがとう、それについては決して教えられなかった:D – FireCross

関連する問題