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 回答
0

可能我没看懂你的问题...

你只想看到一个网格,不是吗?

也许您需要了解 System.out 的概念 System.out 不是您可以编辑的动态工作表。你只会写写写!!逐个追加!

做到这一点的唯一方法是“清屏”并再次绘制网格!因为您无法更改输出中生成的代码!

所以试试这个解决方案!在你的程序中插入这个函数:

private static void Clear()
{
    try
    {
        String os = System.getProperty("os.name");

        if (os.contains("Windows"))
        {
            Runtime.getRuntime().exec("cls");
        }
        else
        {
            Runtime.getRuntime().exec("clear");
        }
    }
    catch (Exception exception)
    {
    }
}

而不是在 drawGrid() 方法之后调用此 Clear() 方法函数,如下所示:

drawGrid(grid);
Clear();
populateRandom(grid,options);

无论如何,看看输出控制台背后的概念!

于 2013-11-15T09:59:00.990 回答