0

わかりましたので、私は「眠れない、プログラミングしたほうがいい」ような夜を過ごしています。今のところ、私はおそらく疲れすぎているだけかもしれませんが、それは私を悩ませています. コードで char[] オプションを 8x8 配列に出力しようとしています。メソッドをセットアップしましたが、別の 8 x 8 配列のすぐ下にある別の 8 x 8 配列に出力してしまいます。警告これはおそらく非常に愚かな間違いです。しかし、私は今、すべてのエラーに気付いていないようです。どんな助けでも大歓迎です!ありがとう!

'

static char[] options = {'*','$','@','+','!','&'};

public static void main(String[] args){ 

    System.out.println("How to play: type a row and column index, followed by \n" +
                        "the direction to move it: u (up), r (right), d (down), l (left)");     
    char[][] grid = new char[8][8];

    drawGrid(grid);
    populateRandom(grid,options);
    System.out.println("Enter <row> <column> <direction to move> or q to quit");
    //char randomChar = 66;
    //System.out.println(randomChar);
}

   public static void drawGrid(char[][] grid){

        System.out.println("\t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8");
        System.out.println();
        //Random r = new Random();

        for(int row=0 ; row < grid.length ; row++) {
            //populateRandom(grid,options);
            System.out.print((row+1)+"");
            for(int column=0 ; column < grid[row].length ; column++){

                //populateRandom(grid, options);

            }
            System.out.println();
        }
            System.out.println();
    }

   public static void populateRandom(char[][] grid, char[] options) 
   {
   Random randomGenerator = new Random();
  for (int row = 0; row < grid.length; row++){
       for (int column=0; column < grid[row].length; column++){

          int randomIndex = randomGenerator.nextInt(6);
          grid[row][column] = options[randomIndex];
          System.out.print("\t" + grid[row][column]);
       }
      System.out.println(); 

   }



   }

   public static boolean isWithinGrid (int row, int col, String cmd, char[][] grid) {
      return true;   
   }

   public static void swap (int row, int col, String cmd, char[][] grid){

   }

   public static boolean updateGrid (char[][] grid, char[] options) {
       return false;
   }

}

'

4

2 に答える 2

0

これを行うにはいくつかの方法があります。私が考えることができる最も簡単な方法は、populateRandom()関数からループを削除し(おそらく別の名前を付けて)、最も内側のループ内で呼び出すことですdrawGrid()

static char[] options = { '*', '$', '@', '+', '!', '&' };

public static void main(String[] args) {

    System.out
            .println("How to play: type a row and column index, followed by \n"
                    + "the direction to move it: u (up), r (right), d (down), l (left)");
    char[][] grid = new char[8][8];

    drawGrid(grid);
    System.out
            .println("Enter <row> <column> <direction to move> or q to quit");
    // char randomChar = 66;
    // System.out.println(randomChar);
}

public static void drawGrid(char[][] grid) {

    System.out.println("\t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8");
    System.out.println();
    // Random r = new Random();

    for (int row = 0; row < grid.length; row++) {
        // populateRandom(grid,options);
        System.out.print((row + 1) + "");
        for (int column = 0; column < grid[row].length; column++) {

            populateRandom(grid[row][column]);

        }
        System.out.println();
    }
    System.out.println();
}

public static void populateRandom(char grid) {
    Random randomGenerator = new Random();
    int randomIndex = randomGenerator.nextInt(6);
    grid = options[randomIndex];
    System.out.print("\t" + grid);

}

public static boolean isWithinGrid(int row, int col, String cmd,
        char[][] grid) {
    return true;
}

public static void swap(int row, int col, String cmd, char[][] grid) {

}

public static boolean updateGrid(char[][] grid, char[] options) {
    return false;
}

あなたがしていることの問題は、コンソールでは、一度新しい行に移動すると、前の行に戻ってそれ以上の内容を出力できないことです (実際にはできますが、それは本当に面倒で無意味です)。

于 2013-11-15T09:51:53.097 に答える