所以我今天花了几个小时写逻辑,把它变成代码,但我完全卡在这一点上,我不知道该怎么办。我现在才用 java 编程几个月,所以整个“逻辑”思维方式还没有完全成熟。谁能帮我思考如何在java中创建一个ulam螺旋的逻辑?
import java.util.Arrays;
public class GridMaker {
private static int gridRow = 5; // R = length
private static int gridCol = 5; // C = height
private static int[][] grid = new int[gridRow][gridCol];
private static int totalSteps = (gridRow * gridCol); // total blocks on the grid
private static int location = 1; // location refers to the number in the box, ie. 1, 2, 3, etc.
private static int rowLength = 1;
public static void main(String[] args) {
grid[Calc.findArrayCenter(gridRow)][Calc.findArrayCenter(gridRow)] = 1;
rowBrowser();
colBrowser();
for (int r = 0; r < gridRow; r++){
for (int c = 0; c < gridCol; c++){
System.out.print(grid[r][c] + " ");
}
System.out.println("");
}
}
public static void rowBrowser() {
int rowCount = 1;
int x = 1;
int stepsInvolved = 2;
if (x < stepsInvolved) {
if (Calc.isOdd(rowCount) == true) {
grid[Calc.findArrayCenter(gridRow)][Calc.findArrayCenter(gridCol) + x] = location + 1;
stepsInvolved++;
}
}
location++;
x++;
}
private static void colBrowser() {
}
}
public class Calc {
public static int findArrayCenter(int center) {
int fcenter = 0;
if (center % 2 != 0)
fcenter = (int) ((center / 2));
else
fcenter = (center / 2);
return fcenter;
}
public static boolean isOdd(int num) {
boolean result = true;
if (num % 2 == 0)
result = false; // false = even, true = odd
return result;
}
}
在这一点上,我需要做什么才能完成创建 ulam 螺旋?我现在正在做的是让数组跟踪一个位置,遍历一行的每个步骤,然后下拉并遍历一列中的步骤,然后将每个计数器加 1 并继续。帮助?对于糟糕的格式感到抱歉,这个网站在粘贴代码方面并没有太大帮助......:|