[英]How to make a "border" of 1s around matrix of 10 by 10 0s in a Java matrix (2d array)
I am new to java, so, please respond with clear and direct answers.我是 Java 新手,所以,请以明确而直接的答案回应。 I made a 12 by 12 matrix of 0s.
我制作了一个 12 x 12 的 0 矩阵。
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
I want to surround the middle area with a "fence" of 1s.我想用1s的“围栏”包围中间区域。
111111111111
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
111111111111
I am really new to Java so I only know how to make the 12 by 12 and print it, but now how to make the fence.我对 Java 真的很陌生,所以我只知道如何制作 12 x 12 并打印它,但现在如何制作围栏。 I also want to know how I could randomly spawn in 10 random 1s inside of the 10 by 10 grid of 0s using a loop.
我还想知道如何使用循环在 10 x 10 的 0 网格内随机生成 10 个随机 1。 Please help!!!!
请帮忙!!!! Here is my code:
这是我的代码:
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
}
}
I have a slightly different suggestion here: don't put the border into your data.我在这里有一个稍微不同的建议:不要将边框放入您的数据中。
What if you want to have "*" tomorrow instead of those 1s?!如果你明天想要“*”而不是那些 1 怎么办?! Then you are broken, because you have an array of int, not of chars?!
然后你就坏了,因为你有一个 int 数组,而不是字符数组?!
Meaning: you should actually separate your data and the printing format .意思是:您实际上应该将数据和打印格式分开。
Sure, that makes the printing more complicated (or not, see below), but your approach does have a lot of ugly consequences.当然,这会使打印更加复杂(或不复杂,请参见下文),但是您的方法确实会产生很多丑陋的后果。 For example: what if you want to pass that matrix to some "computation" code at some point?
例如:如果您想在某个时候将该矩阵传递给某些“计算”代码怎么办? Then that computation code will have to know where/how your "border" sits in that data, to exclude it from its computation.
然后该计算代码必须知道您的“边界”在该数据中的位置/方式,以将其从计算中排除。 Or when you decide to turn your "text console" program into a GUI application?
或者当您决定将“文本控制台”程序转换为 GUI 应用程序时? Then you will probably really draw a border line ... but you will keep carrying around your matrix that includes those "border line" chars (pun intended).
然后你可能真的会画一条边界线......但是你会继续携带包含那些“边界线”字符(双关语)的矩阵。
Thus, why not go:因此,为什么不去:
printTopLine();
for (int[] rows : gameboard) {
printRow(row);
where在哪里
public void printTopLine() {
...println("1111...
}
and和
public void printRow(int[] values) {
print("1");
for (int v : values) print(v);
print("1");
}
As you can see: this is actually very straight forward, and whenever you decide: I want to print things in a different way, you just go in and change thing were necessary, without ever worrying about the data within your 2D array!正如你可以看到:这其实是非常简单的,而当你决定:我要打印的东西以不同的方式,你只是去和变化的东西是必要的,而不用担心你的二维数组中的数据!
So, long story short: you could change your code so that your 2D matrix is only about the values of your game board, and how to "display" that is completely done by other code!所以,长话短说:您可以更改代码,使您的 2D 矩阵仅与游戏板的值有关,以及如何“显示”完全由其他代码完成!
A couple of things here:这里有几件事:
Inner loop should iterate over inner length:内部循环应该遍历内部长度:
for(int y = 0; y < gameboard[0].length; y++)
instead of:代替:
for(int y = 0; y < gameboard.length; y++)
You cannot assign array to int
, types must match.您不能将数组分配给
int
,类型必须匹配。
gameboard[0][y] = gameboard[1];
This won't do what you want: gameboard[x][y] = x + y;
这不会做你想要的:
gameboard[x][y] = x + y;
. . It assigns a sum of indices.
它分配索引的总和。
Since it's a square (both lengths equal), you can iterate once:由于它是一个正方形(两个长度相等),您可以迭代一次:
for (int x = 0; x < gameboard.length; x++) {
gameboard[0][x] = 1;
gameboard[11][x] = 1;
gameboard[x][0] = 1;
gameboard[x][11] = 1;
}
Before that you were doing the same work gameboard.length
times.在此之前,你正在做同样的工作
gameboard.length
次。
You should also print it properly:您还应该正确打印它:
for (int[] a : gameboard) {
for (int x :a) {
System.out.print(x);
}
System.out.println();
}
public static void main(String[] args) {
char border = '1';
char fillCharaters = '0';
int gridLength = 12;
int gridWidth = 12;
char[][] arr = new char[12][12];
Random random = new Random();
for (int j=0; j<gridWidth ; j++){
for (int i=0;i<gridLength;i++){
if(j==0||j==gridWidth-1||i==0||i==gridLength-1){
arr[j][i] = border;
} else {
arr[j][i] = fillCharaters;
}
}
//To print the random characters in the 10X10 grid
int r = random.nextInt(11);
arr[j][r] = border;
}
print2DMatrix(arr);
}
public static void print2DMatrix(char mat[][])
{
for (int i = 0; i < mat.length; i++)
{
for (int j = 0; j < mat[i].length; j++){
if(j==mat[i].length-1){
System.out.println(mat[i][j]);
} else{
System.out.print(mat[i][j]);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.