Java编写 开关灯游戏的二维数组 如图显示 代码在图下面
作者:互联网
**
Java编写 开关灯游戏的二维数组如图显示 代码在图下面
**
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int[][] arrays = { { 0, 1, 1, 0, 0, 0, 0 }, { 1, 1, 0, 1, 0, 0, 0 }, { 1, 0, 1, 0, 0, 0, 1 },
{ 0, 1, 0, 0, 0, 1, 0 }
};
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
System.out.print(arrays[i][j]+" ");
}
System.out.println();
}
int sum=1;
// 循环一下代码
while(sum>0) {
sum=0;
System.out.println("请输入行");
int i=scanner.nextInt();
System.out.println("请输入列");
int j=scanner.nextInt();
//修改坐标
if (i-1>=0) { //上面修改
arrays[i-1][j]=1-arrays[i-1][j];
}
if(i+1<arrays.length) { //修改下面
arrays[i+1][j]=1-arrays[i+1][j];
}
if(j-1>=0) {//修改左边
arrays[i][j-1]=1-arrays[i][j-1];
}
if(j+1<arrays[i].length) {//修改 右边
arrays[i][j+1]=1-arrays[i][j+1];
}
for (int j2 = 0; j2 < arrays.length; j2++) {
for (int k = 0; k < arrays[j2].length; k++) {
System.out.print(arrays[j2][k] + " ");
}
System.out.println();
}
for (int j2 = 0; j2 < arrays.length; j2++) {
for (int k = 0; k < arrays[j2].length; k++) {
sum+=arrays[j2][k];
}
}
}
System.out.println("游戏结束");
}
}
标签:Java,arrays,++,System,j2,int,二维,如图,out 来源: https://blog.csdn.net/weixin_45185519/article/details/120752229