在Java中创建魔术广场 [英] Creating a Magic Square in java

查看:85
本文介绍了在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:

  1. 向用户询问一个奇数
  2. 创建一个n×n数组.
  3. 按照以下步骤创建一个魔术方块.
    一种.在第一行的中间放置一个1.
    b.从行中减去1并加1 列.
    一世.如果可能,将下一个数字放在该位置.
    ii.如果不可能,请按照下列步骤操作.
  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. 如果在第-1行,则更改为最后一行
  2. 如果在最后一栏中更改为第一列
  3. 如果被阻止,则从原始位置下拉至下一行
  4. 如果在右上角,则下拉至下一行.
  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.

  • 打印数组
  • Print the array
  • 我已经编写了代码,但是当我运行它时,程序将输入除第二个数字以外的所有数字;由于某种原因,我的程序跳过了它.例如,如果我将数字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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆