【问题标题】:Print in the 8 x 8 array在 8 x 8 阵列中打印
【发布时间】:2013-11-15 09:37:55
【问题描述】:

好的,所以我有一个“睡不着,不妨编程”之类的夜晚。截至目前,我可能只是过度疲劳,但是,它困扰着我。我试图让我的代码将 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;
   }

}

'

【问题讨论】:

    标签: java arrays loops printing grid


    【解决方案1】:

    有几种方法可以做到这一点。我能想到的最简单的方法是从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;
    }
    

    您所做的事情的问题是,在控制台上,一旦您转到新行,就无法返回并在前一行打印更多内容(实际上可以,但这确实很麻烦且毫无意义)。

    【讨论】:

      【解决方案2】:

      也许我不明白你的问题...

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

      也许你需要了解 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);
      

      不管怎样,看看输出控制台背后的概念吧!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-22
        • 1970-01-01
        • 2012-01-07
        • 1970-01-01
        • 2012-03-08
        • 2021-11-08
        • 2016-12-03
        相关资源
        最近更新 更多