在 java 中创建幻方
Creating a Magic Square in java
我必须编写一个程序,从用户那里接收一个奇数并创建一个幻方。幻方是每行、每列和对角线的总和相同的幻方。这些是编写代码的特点:
- Ask the user for an odd number
- Create an n by n array.
- Follow these steps to create a magic square.
a. Place a 1 in the middle of the first row.
b. Subtract 1 from the row and add 1
to the column.
i. If possible place the next number at that position.
ii. If not possible, follow these steps.
- If in row -1, then change to last row
- If in last column change to first column
- If blocked, then drop down to next row (from original position)
- if in the upper right corner, then drop down to next row.
- Print the array
我已经编写了代码,但是当我 运行 它时,程序会输入除二之外的所有数字;出于某种原因,我的程序跳过了它。例如,如果我输入数字 3 作为奇数,我的输出是:
6 1 0
3 4 5
9 7 8
0 不应该在那里,但第二个是。这是我的代码:
public static void main(String[] args) {
System.out.print("Give an odd number: ");
int n = console.nextInt();
int[][] magicSquare = new int[n][n];
int number = 1;
int row = 0;
int column = n / 2;
while (number <= n * n) {
magicSquare[row][column] = number;
number++;
row -= 1;
column += 1;
if (row == -1) {
row = n - 1;
}
if (column == n) {
column = 0;
}
if (row == 0 && column == n - 1) {
column = n - 1;
row += 1;
} else if (magicSquare[row][column] != 0) {
row += 1;
}
}
for (int i = 0; i < magicSquare.length; i++) {
for (int j = 0; j < magicSquare.length; j++) {
System.out.print(magicSquare[i][j] + " ");
}
System.out.println();
}
}
谁能告诉我哪里出错了,为什么我的程序会跳过数字 2? *这是一个家庭作业问题,所以请只回答代码。谢谢
你方块的逻辑确保右上角永远不会被写入。
if in the upper right corner, then drop down to next row.
这是执行此操作的代码...
if (row == 0 && column == n - 1) {
column = n -1;
row += 1;
因此,在下一次迭代中输入任何值之前,您总是要离开该位置。您实际上不需要将列设置为 n - 1 的那一行,因为根据其上方的逻辑定义,它已经是 n - 1。
您实际上是将值 2 写入第二行第三列。它稍后会被值 5 覆盖。如果您在程序的每次迭代后输出数组的值,那么您将看到模型的状态是如何变化的。
删除 3.4 可能会修复您的代码。
public static void main(String[] args) {
System.out.print("Give an odd number: ");
int n = console.nextInt();
int[][] magicSquare = new int[n][n];
int number = 1;
int row = 0;
int column = n / 2;
int curr_row;
int curr_col;
while (number <= n * n) {
magicSquare[row][column] = number;
number++;
curr_row = row;
curr_col = column;
row -= 1;
column += 1;
if (row == -1) {
row = n - 1;
}
if (column == n) {
column = 0;
}
if (magicSquare[row][column] != 0) {
row = curr_row + 1;
column = curr_col;
if (row == -1) {
row = n - 1;
}
}
}
for (int i = 0; i < magicSquare.length; i++) {
for (int j = 0; j < magicSquare.length; j++) {
System.out.print(magicSquare[i][j] + " ");
}
System.out.println();
}
}
设置 n = 3 得到以下似乎正确的输出。
8 1 6
3 5 7
4 9 2
我必须编写一个程序,从用户那里接收一个奇数并创建一个幻方。幻方是每行、每列和对角线的总和相同的幻方。这些是编写代码的特点:
- Ask the user for an odd number
- Create an n by n array.
- Follow these steps to create a magic square.
a. Place a 1 in the middle of the first row.
b. Subtract 1 from the row and add 1 to the column.
i. If possible place the next number at that position.
ii. If not possible, follow these steps.
- If in row -1, then change to last row
- If in last column change to first column
- If blocked, then drop down to next row (from original position)
- if in the upper right corner, then drop down to next row.
- Print the array
我已经编写了代码,但是当我 运行 它时,程序会输入除二之外的所有数字;出于某种原因,我的程序跳过了它。例如,如果我输入数字 3 作为奇数,我的输出是:
6 1 0
3 4 5
9 7 8
0 不应该在那里,但第二个是。这是我的代码:
public static void main(String[] args) {
System.out.print("Give an odd number: ");
int n = console.nextInt();
int[][] magicSquare = new int[n][n];
int number = 1;
int row = 0;
int column = n / 2;
while (number <= n * n) {
magicSquare[row][column] = number;
number++;
row -= 1;
column += 1;
if (row == -1) {
row = n - 1;
}
if (column == n) {
column = 0;
}
if (row == 0 && column == n - 1) {
column = n - 1;
row += 1;
} else if (magicSquare[row][column] != 0) {
row += 1;
}
}
for (int i = 0; i < magicSquare.length; i++) {
for (int j = 0; j < magicSquare.length; j++) {
System.out.print(magicSquare[i][j] + " ");
}
System.out.println();
}
}
谁能告诉我哪里出错了,为什么我的程序会跳过数字 2? *这是一个家庭作业问题,所以请只回答代码。谢谢
你方块的逻辑确保右上角永远不会被写入。
if in the upper right corner, then drop down to next row.
这是执行此操作的代码...
if (row == 0 && column == n - 1) {
column = n -1;
row += 1;
因此,在下一次迭代中输入任何值之前,您总是要离开该位置。您实际上不需要将列设置为 n - 1 的那一行,因为根据其上方的逻辑定义,它已经是 n - 1。
您实际上是将值 2 写入第二行第三列。它稍后会被值 5 覆盖。如果您在程序的每次迭代后输出数组的值,那么您将看到模型的状态是如何变化的。
删除 3.4 可能会修复您的代码。
public static void main(String[] args) {
System.out.print("Give an odd number: ");
int n = console.nextInt();
int[][] magicSquare = new int[n][n];
int number = 1;
int row = 0;
int column = n / 2;
int curr_row;
int curr_col;
while (number <= n * n) {
magicSquare[row][column] = number;
number++;
curr_row = row;
curr_col = column;
row -= 1;
column += 1;
if (row == -1) {
row = n - 1;
}
if (column == n) {
column = 0;
}
if (magicSquare[row][column] != 0) {
row = curr_row + 1;
column = curr_col;
if (row == -1) {
row = n - 1;
}
}
}
for (int i = 0; i < magicSquare.length; i++) {
for (int j = 0; j < magicSquare.length; j++) {
System.out.print(magicSquare[i][j] + " ");
}
System.out.println();
}
}
设置 n = 3 得到以下似乎正确的输出。
8 1 6
3 5 7
4 9 2