Java 幻方发生器

Java 幻方发生器,java,arrays,algorithm,Java,Arrays,Algorithm,我试图开发一个程序,创建并打印一个nxn“幻方”,其中n是用户生成的奇数整数。但是,我一直收到相同的错误“java.lang.ArrayIndexOutOfBoundsException:3” 算法摘要: 将1放置在第一行(第0行)的中心 根据以下规则放置2,3,4…n 向上和向右移动到新位置(行,列),即行=行-1,列=列+1 如果row=-1(row不在数组中),则将数字放在(n-1,col)中的最后一行/底行中 如果col=n(列不在数组中),则将数字放入(行,0) 从上角方格移动时,将下

我试图开发一个程序,创建并打印一个nxn“幻方”,其中n是用户生成的奇数整数。但是,我一直收到相同的错误“java.lang.ArrayIndexOutOfBoundsException:3”

算法摘要:

  • 将1放置在第一行(第0行)的中心

  • 根据以下规则放置2,3,4…n

  • 向上和向右移动到新位置(行,列),即行=行-1,列=列+1 如果row=-1(row不在数组中),则将数字放在(n-1,col)中的最后一行/底行中

    如果col=n(列不在数组中),则将数字放入(行,0)

    从上角方格移动时,将下一个数字放在左下角方格中。如果该位置已确定,则将新数字放在上一个数字下

    import java.util.*;
    public class MagicSquare
    {
    public static void main (String[] args)
    {
    Scanner input = new Scanner(System.in);
    
    System.out.println("Enter an odd integer:");
    
    int n = input.nextInt();
    
    int[][] magic = new int[n][n];
    
    int row = 0;
    int col = (n-1)/2;
    magic[row][col] = 1;
    
     for(int i=2; i <=n*n; i++)
    {
       if(magic[row-1][col+1]==0){
         row=row-1;
         col=col+1;
       }
       else{
         row=row+1;
       }
       if(row==-1)
         row = n-1;
       if (col== n)
         col=0;
       magic[row][col]=i;
    }
    for(int x = 0; x<n; x++)
    {
      for(int y=0; y<n; y++)
        System.out.print("|"+magic[x][y] +"|\t");
      System.out.println();
     }
    }
    }
    
    import java.util.*;
    公共级魔法广场
    {
    公共静态void main(字符串[]args)
    {
    扫描仪输入=新扫描仪(System.in);
    System.out.println(“输入一个奇数整数:”);
    int n=input.nextInt();
    int[]magic=新的int[n][n];
    int行=0;
    int col=(n-1)/2;
    魔术[行][列]=1;
    
    对于(int i=2;i在
    中,对于
    循环,您将
    重置到末尾:

    if(row==-1)
      row = n-1;
    
    但在下一次迭代中,您将使用
    行+1
    索引:

    if(magic[row+1][col+1]==0)
    
    它将结束。

    因此,遵循您的规则(感谢您重新设置它们的格式!):

    publicstaticvoidmain(字符串[]args)
    {
    扫描仪输入=新扫描仪(System.in);
    System.out.println(“输入一个奇数整数:”);
    int n=input.nextInt();
    int[]magic=新的int[n][n];
    //将1放置在第一行(第0行)的中心
    int行=0;
    int col=(n-1)/2;
    魔术[行][列]=1;
    
    对于(int i=2;我可以请您重新格式化规则吗?我在解析规则时遇到了问题-尤其是最后的位。谢谢,这当然是问题的一部分。我相信我已经更正了它,我已经在上面的问题中添加了更新的循环。但是,我仍然遇到错误“java.lang.ArrayIndexOutOfBoundsException:-1”有什么建议吗?
    public static void main (String[] args)
    {
        Scanner input = new Scanner(System.in);
    
        System.out.println("Enter an odd integer:");
    
        int n = input.nextInt();
    
        int[][] magic = new int[n][n];
    
        // Place 1 in the center of the first row ( row 0)
        int row = 0;
        int col = (n-1)/2;
        magic[row][col] = 1;
    
        for(int i=2; i <=n*n; i++)
        {
           // Move up and right to a new position
           col = col + 1;
           row = row - 1;
           // row is off the array
           if (row == -1)
               row = n - 1;
           // col is off the array
           if (col == n)
               col = 0;
           // if the place is taken, place the new number under the previous number
           if( magic[row][col] !=0 )
           {
             row = row + 1;
             if (row == n)
               row = 0;
           }
    
           magic[row][col]=i;
        }
    
        for(int x = 0; x < n; x++)
        {
          for(int y=0; y < n; y++)
            System.out.print("|"+magic[x][y] +"|\t");
          System.out.println();
        }
    }