如何仅使用一维数组生成魔方?

时间:2015-10-29 23:28:49

标签: java arrays magic-square

我被指派提示用户给出魔方的顺序(3级魔方将是3x3矩阵),然后生成该顺序的魔方,而不使用2维数组。

这是我所了解和理解的:

  • 数组将包含订单平方元素。
  • 当您打印出表格时,数组中的数字位置为[order×row + col]
  • 表中数组位置的索引是(索引/顺序,索引%顺序)

以下是我给出的内容,但不明白如何正确实施:

  1. row = order - 1,col = order / 2,i = 1
  2. 重复以下操作,直到i = order ^ 2 + 1:

    (a)魔术[index] = i

    (b)将row和col递增1.即,row =(row + 1)mod order和col =(col + 1)%order

    (c)如果magic [index]!= 0

       i. row = (row + order − 2) % order
    
       ii. col = (col + order − 1) % order
    

    (d)将i增加1

  3. 到目前为止,这是我的代码:

    package magicsquarecreator;
    
    import java.io.IOException;
    import java.util.Scanner;
    
    
    public class MagicSquareDemo
    {
       public static void main(String[] args) 
       {
          Scanner keyIn = new Scanner(System.in); 
          String option; 
          do
          {
             System.out.println();        
             System.out.println("             MAGIC SQUARE APPLICATION             ");
             System.out.println("==================================================");
             System.out.println("Generate a Magic Square........................[1]");
             System.out.println("Test for a Magic Square........................[2]");
             System.out.println("Quit the Program...............................[0]");
             System.out.println();
             System.out.print("Select an option -> ");
             option = keyIn.next(); 
             System.out.println(); 
    
             switch(option)
             {
                case "1": 
                   try
                   {
                        System.out.println("Enter the dimension of the magic square -> ");
                        int order = keyIn.nextInt();
                        if (order < 1)
                        {
                            System.out.println("The order must be positive and odd");
                        }
                        else if (order % 2 == 0)
                        {
                            System.out.println("The program can only generate a magic square"
                                    + "\nwhose dimension is positive odd number.");
                        }
                        else if (order % 2 != 0)
                        {
                            int i=1, j=1;
                            int row = order - 1;
                            int col = order/2;
                            int[] magic = new int[order*order];
                            for (i=1; i<=order; i++)
                            {
                                magic[i] = i;
                                row = (row + 1) % order;
                                col = (col + 1) % order;
                                if (magic[i] != 0)
                                {
                                  row = (row + order − 2) % order;
                                  col = (col + order − 1) % order;
                                }
                            }
                        }
                   }
                   catch(IOException e)
                   {
                      System.out.println(e);
                   }
                   break;                    
                case "2": 
                   try
                   {
    
    
                   }
                   catch(IOException e)
                   {
                      System.out.println(e);
                   }
                   break;    
                case "0": 
                   break;    
                default: System.out.println("Invalid choice...please select a valid menu option.");
             }
          } 
          while (option.compareTo("0") != 0); 
       } 
    }
    

    现在我只是担心获取和理解案例1. Try Catch语句用于文件输出,但我可以在我自己的时间做到这一点。目前最大的问题是了解如何创建此循环。

1 个答案:

答案 0 :(得分:0)

说实话,我也没有真正理解这些,因为它是从你写的。如果我这样做,因为我理解它,它不起作用。 但我看起来它应该如何工作,最后我明白了。 这是作为方法的else if (order % 2 != 0)条件的一部分(我测试了它,它现在正在工作):

public static void magicSqare(int order){
    System.out.println(order);
    int row = order - 1;
    int col = order /2;
    int [] magic = new int [order*order];
    int index;

    index = (row)*order + col;
    magic[index]= 1;

    for (int i = 2; i <= order*order; i++) {
        if (magic[((row +1) % order)*order + ((col +1) % order)] == 0) {
        row = (row +1) % order;
        col = (col +1) % order;
        }

        else {
            row = (row + order -1) % order;
            //col = (col + order -1) % order;
        }
        index = (row)*order + col;
        magic[index]= i;
    }

    //System.out.println(Arrays.toString(magic));
}