Ai
1 Star 0 Fork 0

kee/100-examples-of-Java-interesting-programming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ch1_8.java 2.17 KB
一键复制 编辑 原始数据 按行查看 历史
😊wenkai 提交于 2023-12-14 17:21 +08:00 . 新建仓库-push
//创建回形矩阵
/**
* 具体思路:
* 一共三个部分,填充数字、确定方向、打印数字
* 使用switch根据方向填充数字(二维数组)
* 通过判断转弯条件实现控制方向
* 利用循环打印二维数组
*/
public class ch1_8{
static enum Direction{
Right,Down,Left,Up;
}//方向是常量,用枚举
//创建填充数字方法
public static void initialArray() {
int row=0, col=0;//第一个数字
for (int c=0; c<length*length;c++) {
snake[row][col] = value;
lastDirection = findDirection(row,col);
//根据方向,填充数字
switch (lastDirection) {
case Right:
col++;
break;
case Down:
row++;
break;
case Left:
col--;
break;
case Up:
row--;
break;
default:
System.out.println("error");
}
value++;
}
}
//创建确定下一步方向的方法
static Direction findDirection(int row, int col) {
Direction direction = lastDirection;
switch (direction) {
case Right:{ //如果向右到尽头则向下
if (col == length-1 || snake[row][col+1] !=0)
direction = direction.Down;
break;
}
case Down:{ //如果向下到尽头则向左
if (row == length-1 || snake[row+1][col] !=0)
direction = direction.Left;
break;
}
case Left:{ //如果向左到尽头则向上
if (col == 0 || snake[row][col-1] !=0)
direction = direction.Up;
break;
}
case Up:{ //如果向上到尽头则向右
if (snake[row-1][col] !=0)
direction = direction.Right;
break;
}
}
return direction; //返回方向
}
//创建print方法
static void print(int[][] arr) {
for (int i=0; i<length; i++) {
for (int j=0; j<length; j++) {
System.out.printf("%5d",arr[i][j]); //每个数字占5位
}
System.out.println(); //换行
}
}
//进行初始赋值
static int length = 12; // 确定行数
static int value = 1; // 确定第一个数字
static int[][] snake = new int [length][length]; // 创建二维数组
static Direction lastDirection = Direction.Right; // 确定初始方向:向右
//运行主程序
public static void main(String[] args) {
initialArray(); //填充数字
print(snake); //打印数字
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wizkee/100-examples-of-java-interesting-programming.git
git@gitee.com:wizkee/100-examples-of-java-interesting-programming.git
wizkee
100-examples-of-java-interesting-programming
100-examples-of-Java-interesting-programming
master

搜索帮助