2016-12-02 6 views
1

2から6の間の数値を持つ2D配列をユーザーが指定する必要がありますが(大きなプロジエクトの一部です)、数値を渡すと別の要求があります数。ランダムに2次元配列を塗りつぶす(Java)

public static int[][] crearTablero(int tamaño) 
{ 
    int[][] tablero = new int[tamaño][tamaño]; 
    return tablero; 
} 
public static void imprimeTablero(int[][] tablero) 
{ 
    for(int i = 0; i<tablero.length; i++) 
    { 
     for(int j = 0; j<tablero[i].length; j++) 
     { 
      System.out.print(tablero[i][j] + " "); 
     } 
     System.out.println(); 
    } 
} 
public static void swap(int[][] tablero, int x1, int y1, int x2, int y2) 
{ 
    int temp = tablero[x1][y1]; 
    tablero[x1][y1] = tablero[x2][y2]; 
    tablero[x2][y2] = temp; 
} 
public static void rellenarTablero(int[][] tablero) { 
    for (int x = 0; x < tablero.length; x++) { 
     for (int y = 0; y < tablero[x].length; y++) { 
      tablero[x][y] = aleatorio(numeroColores()); 
     } 
    } 
} 
public static void shuffleBoard(int[][] tablero) 
{ 
    Random rnd = new Random(); 
    int randX = 0; 
    for(int x = 0; x<tablero.length; x++) 
    { 
     randX = rnd.nextInt(tablero.length); 
     int[] temp = tablero[x]; 
     tablero[x] = tablero[randX]; 
     tablero[randX] = temp; 
    } 
} 
public static int numeroColores(){ 
    int colores = 0; 
    System.out.print("Numero de colores (entre 2 y 6): "); 
    Scanner scn = new Scanner(System.in); 
    colores = scn.nextInt(); 
    while(colores < 2 || colores > 6) 
    { 
     System.out.println("Invalid matrix size. Re-enter "); 
    } 
    return colores; 
} 
public static int aleatorio(int colores) { 
    int l = (int) (Math.floor(Math.random()*(colores-2)) + 2); 
    return l; 
    } 

私は、感謝を継続する方法を知らないので、私は実際にいくつかの助けをいただければ幸いです。

+0

デバッガを接続して自分自身を見つけてください。 "なぜこのコードは動作していないのですか"は、SOのオフトピックです:http://stackoverflow.com/help/how-to-ask – Vampire

答えて

0

for-loopでforループでnumeroColores()を呼び出したので、もちろん複数回尋ねられます。

Btw。あなたは常にプリントアウトし、同じ行を取得して1以下または7または大きなで入力すると、無限ループを持っていない新しい入力を求め

+0

これは問題でした。ありがとう、今は働いています:D –

+0

http://stackoverflow.com/help/someone-answersに従ってください – Vampire

0

2と6

public static int aleatorio(int colores) { 
    int l = 0; 
    while(l < 2 || l > 6) { 
     l = (int) (Math.floor(Math.random()*(colores-2)) + 2); 
    } 
    return l; 
} 
間のランダムな値を生成するには、このコードを試してみてください
関連する問題