package jishuhuanfang;
import java.io.IOException;
public class constant {
int arr[][];
int i,j;
int k=1;
void jishu(int n)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
arr[i][j]=0;
j=(n+1)/2-1;
i=0;
while(k<=n*n)
{
if(arr[i][j]!=0)
i=i+1;
arr[i][j]=k;
k=k+1;
if(j+1==n&&i-1==-1)
i=i+1;
else if(i-1==-1)
{
i=n-1;
j=j+1;
}
else if(j+1==n)
{
j=0;
i=i-1;
}
else
{
j=j+1;
i=i-1;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.print("\n");
}
}
public static void main(String[] args) throws IOException {
constant test=new constant();
System.out.println("输入n:");
int n=System.in.read();
test.jishu(n);
/*
if(n%2==1)
//奇数阶幻方
else if(n==2)
System.out.println("不存在2阶幻方");
else if(n%4==0)
//4k偶数阶幻方
else
//4k+2偶数阶幻方
*/
}
}
输入n:
5
Exception in thread "main" java.lang.NullPointerException
at jishuhuanfang.constant.jishu(constant.java:13)
at jishuhuanfang.constant.main(constant.java:56)
我该怎么修改呢?