如何在 Java 矩阵(二维数组)中的 10 x 10 0 矩阵周围制作 1 的“边界”

我是java新手,所以,请用清晰直接的答案回应。我制作了一个 12 x 12 的 0 矩阵。


000000000000

000000000000

000000000000

000000000000

000000000000

000000000000

000000000000

000000000000

000000000000

000000000000

000000000000

000000000000

我想用1秒的“栅栏”包围中间区域。


111111111111

100000000001

100000000001

100000000001

100000000001

100000000001

100000000001

100000000001

100000000001

100000000001

100000000001

111111111111

我对 Java 很陌生,所以我只知道如何制作 12 x 12 并打印它,但现在如何制作栅栏。我还想知道如何使用循环在 10 x 10 0 网格内随机生成 10 个随机 1 。请帮忙!!!!这是我的代码:


class Main {

 public static void main(String[] args){

 int[][] gameboard = new int[12][12];// make gameboard

 int r_length = gameboard.length;//row length

 int c_length = gameboard[0].length;//column length

 for (int x = 0; x < gameboard.length; x++) {

   for(int y = 0; y < gameboard.length; y++){

     gameboard[x][y] = x + y;

     gameboard[0][y] = gameboard[1]; //replace row 1 (not working)

     gameboard[11][y] = gameboard[1]; //replace row 12 (not working)

     gameboard[x][0] = gameboard[1]; //replace column 1 (not working)

     gameboard[x][11] = gameboard[1]; //replace column 12 (not working)

    }

  }


 for (int[] a : gameboard) {

   for (int x :a) {

     System.out.print("0");

    }

   System.out.println();

  }//gameboard printing

 }

}


慕盖茨4494581
浏览 130回答 3
3回答

狐的传说

我在这里有一个稍微不同的建议:不要将边框放入数据中。如果你想明天有“*”而不是那些 1 怎么办?!那么你就崩溃了,因为你有一个 int 数组,而不是 chars 数组?!含义:您实际上应该将数据和打印格式分开。当然,这会使打印变得更加复杂(或者不会,见下文),但是你的方法确实会产生很多丑陋的后果。例如:如果您想在某个时刻将该矩阵传递给某些“计算”代码怎么办?然后,该计算代码必须知道您的“边界”在该数据中的位置/方式,以将其从计算中排除。或者当您决定将“文本控制台”程序转变为 GUI 应用程序时?然后你可能真的会画一条边界线......但你会继续携带包含那些“边界线”字符(双关语)的矩阵。因此,为什么不去:printTopLine();for (int[] rows : gameboard) {&nbsp; printRow(row);在哪里public void printTopLine() {&nbsp; &nbsp;...println("1111...}和public void printRow(int[] values) {&nbsp; &nbsp;print("1");&nbsp; &nbsp;for (int v : values) print(v);&nbsp; &nbsp;print("1");}正如您所看到的:这实际上非常简单,每当您决定:我想以不同的方式打印内容时,您只需进入并更改必要的内容,而无需担心 2D 数组中的数据!所以,长话短说:您可以更改代码,以便您的 2D 矩阵仅与游戏板的值有关,以及如何“显示”完全由其他代码完成!

侃侃尔雅

这里有几件事:内部循环应该迭代内部长度:for(int&nbsp;y&nbsp;=&nbsp;0;&nbsp;y&nbsp;<&nbsp;gameboard[0].length;&nbsp;y++)代替:for(int&nbsp;y&nbsp;=&nbsp;0;&nbsp;y&nbsp;<&nbsp;gameboard.length;&nbsp;y++)您不能将数组分配给int,类型必须匹配。gameboard[0][y]&nbsp;=&nbsp;gameboard[1];这不会做你想做的事:gameboard[x][y] = x + y;。它分配索引的总和。由于它是一个正方形(两个长度相等),因此您可以迭代一次:for&nbsp;(int&nbsp;x&nbsp;=&nbsp;0;&nbsp;x&nbsp;<&nbsp;gameboard.length;&nbsp;x++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameboard[0][x]&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameboard[11][x]&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameboard[x][0]&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gameboard[x][11]&nbsp;=&nbsp;1; }在此之前,你们的工作时间是一样的gameboard.length。您还应该正确打印它:&nbsp;for&nbsp;(int[]&nbsp;a&nbsp;:&nbsp;gameboard)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;x&nbsp;:a)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(x); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;System.out.println(); &nbsp;&nbsp;}

慕的地6264312

public static void main(String[] args) {&nbsp; &nbsp; char border = '1';&nbsp; &nbsp; char fillCharaters = '0';&nbsp; &nbsp; int gridLength = 12;&nbsp; &nbsp; int gridWidth = 12;&nbsp; &nbsp; char[][] arr = new char[12][12];&nbsp; &nbsp; Random random = new Random();&nbsp; &nbsp; for (int j=0; j<gridWidth ; j++){&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0;i<gridLength;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(j==0||j==gridWidth-1||i==0||i==gridLength-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j][i] = border;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j][i] = fillCharaters;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //To print the random characters in the 10X10 grid&nbsp; &nbsp; &nbsp; &nbsp; int r = random.nextInt(11);&nbsp; &nbsp; &nbsp; &nbsp; arr[j][r] = border;&nbsp; &nbsp; }&nbsp; &nbsp; print2DMatrix(arr);}public static void print2DMatrix(char mat[][])&nbsp;{&nbsp;&nbsp; &nbsp; for (int i = 0; i < mat.length; i++)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < mat[i].length; j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(j==mat[i].length-1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mat[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(mat[i][j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java