魔術矩陣程式碼...
import java.text.DecimalFormat;
import java.io.*;
class TEST2{ public static void main(String args[])throws IOException{ System.out.print("請輸矩陣大小(不得超過19):");
InputStreamReader ir1 = new InputStreamReader(System.in);
BufferedReader br1 = new BufferedReader(ir1);
String num = br1.readLine(); int n = Integer.parseInt(num);
int m[][] = new int[n][n]; int p = 1;
int c = (n-1)/2; int r = -1; if(n%2 == 1 && n<=19) { System.out.println("您輸入的矩陣大小為" + num + "*" + num + "的矩陣");
for(int i=0;i<n;i++) { r = (r+n+1)%n; m[c][r] = p++;
for(int j=1;j<n;j++) { r = (r+n-1)%n;
c = (c+n+1)%n; m[c][r] = p++; } } DecimalFormat th = new DecimalFormat( "000" );
for (r=0;r<n;r++) { for (c=0;c<n;c++) { System.out.print(" "+th.format(m[c][r])); }
System.out.println(); }
System.out.println(""); }
else System.out.println("\n輸入值為偶數或超出範圍"); } }