2

我被指派提示用户给出幻方的顺序(3 阶幻方将是 3x3 矩阵),然后在不使用二维数组的情况下生成该顺序的幻方。

以下是我所知道和理解的:

  • 数组将具有平方元素。
  • 打印表格时,数组中数字的位置为[order × row + col]
  • 数组在表上的位置的索引是 (index/order, index % order)

这是我得到的,但不明白如何正确实施:

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

    (a) 魔法[索引] = i

    (b) 将 row 和 col 加 1。即 row = (row + 1) mod order 和 col = (col + 1) % order

    (c) 如果魔法[索引] != 0

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

    (d) i 加 1

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

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 语句,但我可以自己做。目前最大的问题是理解如何创建这个循环。

4

1 回答 1

0

老实说,我也没有真正理解 taks,因为它是你写的。如果我按照我的理解去做,它就行不通。但我看了看它应该如何工作,最后我明白了。这是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));
}
于 2015-10-30T02:23:37.067 回答