用java创建一个魔方

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36040292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:23:52  来源:igfitidea点击:

Creating a Magic Square in java

javaarrayswhile-loopmagic-square

提问by ch1maera

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:

我必须编写一个程序,从用户那里接收一个奇数并创建一个幻方。幻方是每行、每列和对角线的总和相同的方。这些是编写代码的特征:

  1. Ask the user for an odd number
  2. Create an n by n array.
  3. 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
    2. If in last column change to first column
    3. If blocked, then drop down to next row (from original position)
    4. if in the upper right corner, then drop down to next row.
  4. Print the array
  1. 向用户询问奇数
  2. 创建一个 n × n 数组。
  3. 按照以下步骤创建一个幻方。
    一种。在第一行的中间放置一个 1。
    湾 行减1,列加1。
    一世。如果可能,将下一个数字放在该位置。
    ii. 如果不可能,请按照以下步骤操作。
    1. 如果在第 -1 行,则更改为最后一行
    2. 如果在最后一列更改为第一列
    3. 如果被阻止,则下拉到下一行(从原始位置)
    4. 如果在右上角,则下拉到下一行。
  4. 打印数组

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:

我已经编写了代码,但是当我运行它时,程序会输入除数字 2 之外的所有数字;出于某种原因,我的程序跳过了它。例如,如果我输入数字 3 作为奇数,我的输出是:

6 1 0 
3 4 5 
9 7 8 

0 isn't supposed to be there but the number two is. Here is my code:

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();
    }
}

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.

有人能告诉我哪里出错了,为什么我的程序跳过了数字 2?*这是一个家庭作业问题,所以请只用代码回答。谢谢。

采纳答案by Brendan

Removing 3.4 will probably fix your code.

删除 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();
    }
}

Setting n = 3 gets me the following output which seems correct.

设置 n = 3 让我得到以下似乎正确的输出。

8 1 6 
3 5 7 
4 9 2 

回答by ManoDestra

The logic of your square ensures that the top right corner is never written to.

正方形的逻辑确保永远不会写入右上角。

if in the upper right corner, then drop down to next row.

if in the upper right corner, then drop down to next row.

And here's the code that does it...

这是执行此操作的代码...

if (row == 0 && column == n - 1) {
    column = n -1;
    row += 1;

So, you're always going to move away from that location before you enter any value on your next iteration. You don't actually need that line that sets the column to n - 1 as it's already n - 1 by definition of the logic above it.

因此,在下一次迭代中输入任何值之前,您总是要离开该位置。您实际上不需要将列设置为 n - 1 的那一行,因为根据上面的逻辑定义,它已经是 n - 1 了。

You're actually writing the value 2 to the second row, third column. It is then later overwritten by the value 5. If you output the values of your array after each iteration of your program, then you'll see how the state of your model is changing.

您实际上是将值 2 写入第二行第三列。然后它被值 5 覆盖。如果在每次程序迭代后输出数组的值,那么您将看到模型的状态如何变化。