在Java中创建魔术广场 [英] Creating a Magic Square in java
问题描述
我必须编写一个程序,该程序需要从用户那里输入一个奇数并创建一个幻方.幻方是指行,列和对角线的总和相同的正方形.这些是编写代码的特征:
I have to write a program that takes in an odd number from the user and creates a magic square. A magic square is one where the sum of each row, column, and diagonal is the same. These are the characteristics for writing the code:
- 向用户询问一个奇数
- 创建一个n×n数组.
- 按照以下步骤创建一个魔术方块.
一种.在第一行的中间放置一个1.
b.从行中减去1并加1 列.
一世.如果可能,将下一个数字放在该位置.
ii.如果不可能,请按照下列步骤操作.
- 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.
- 如果在第-1行,则更改为最后一行
- 如果在最后一栏中更改为第一列
- 如果被阻止,则从原始位置下拉至下一行
- 如果在右上角,则下拉至下一行.
- 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.
我已经编写了代码,但是当我运行它时,程序将输入除第二个数字以外的所有数字;由于某种原因,我的程序跳过了它.例如,如果我将数字3用作奇数,则输出为:
I've written the code but when I run it, the program puts in all the numbers except for the number two; for some reason, my program skips over it. For example, if I put in the number 3 as the odd number, my output is:
6 1 0
3 4 5
9 7 8
不应有0,但第二个是.这是我的代码:
0 isn't supposed to be there but the number two is. Here is my code:
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? *这是一个家庭作业问题,因此代码只能回答.谢谢.
Could someone tell me where I went wrong and why my program is skipping the number 2? *This is a homework question so code only answers, please. Thanks.
推荐答案
删除3.4可能会修复您的代码.
Removing 3.4 will probably fix your code.
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会得到以下看似正确的输出.
Setting n = 3 gets me the following output which seems correct.
8 1 6
3 5 7
4 9 2
这篇关于在Java中创建魔术广场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!