英文:
for loop that cycles between 0 and 1 in a 2D Array
问题
我目前正在制作一个国际象棋棋盘,我需要使用双重循环为每个其他单元格分配1或0。我的代码如下:
int[][] board = new int[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = ?;
println(board[i][j]);
}
}
英文:
I am currently making a chessboard and I need to assign every other cell with 1 or 0 with a double for loop.
My code looks like this:
(Processing / Java)
int[][] board = new int [8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = ?;
println(board[i][j]);
}
}
答案1
得分: 2
看起来你正在尝试为棋盘分配颜色。
0 - 黑色
1 - 白色
你可以这样做:
如果((i + j) % 2 == 0)
arr[i][j] = 0;
否则
arr[i][j] = 1;
英文:
Looks like you're trying to assign color to a chessBoard.
0 - Black
1 - White
what you can do is,
if((i + j) % 2 == 0)
arr[i][j] = 0;
else
arr[i][j] = 1;
答案2
得分: 2
int[][] board = new int[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = (j + i) % 2;
System.out.println(board[i][j]);
}
}
英文:
int[][] board = new int[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = (j+i) % 2;
System.out.println(board[i][j]);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论