奇数幻方 求解java的奇数幻方 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;import java.util.*;public class TestMag { public void Magic(int n){ int a[][]=new int[n][n]; int row,col; row=0;col=(n-1)/2; a[row][col]=1; for(int j=2;j<=n*n;j++){ if ((j-1)%n==0) { row=(row+1)%n; }else{ row=(row+n-1)%n; col=(col+1)%n; } a[row][col]=j; }//end for for(row=0;row<n;row++){ for(col=0;col<n;col++) System.out.print(a[row][col]+" "); System.out.println(); }; } public static void main(String[]args){ int n=0; TestMag tm=new TestMag(); String sn; do{ System.out.print("请输入一个奇数:"); BufferedReader streami = new BufferedReader( new InputStreamReader(System.in)); try { sn=streami.readLine(); n=Integer.parseInt(sn); } catch (Exception ex) { } } while (n%2==0); tm.Magic(n); }} 跟我学spring3系列 word原版 下载 【转】 将文件1.rar复制到为c:/test/2.rar,并将1.rar删除? 求救:关于 控制按钮的问题! JS 和JSP相关 你做开发的时候最喜欢听的音乐 数据结合转换的问题? 执行System.exit(0)操作后 程序没有完全关闭 SQL的异常,为什么我无法连接我的SQL数据库,哭了, 异常处理 真郁闷,散分2 谢谢大家对我的帮助~!真的很感激 JList中加入JCheckBox控件时多选问题请教 resultSet是只读的,如何将它变成可写的?
import java.util.*;
public class TestMag {
public void Magic(int n){
int a[][]=new int[n][n];
int row,col;
row=0;col=(n-1)/2;
a[row][col]=1;
for(int j=2;j<=n*n;j++){
if ((j-1)%n==0) {
row=(row+1)%n;
}else{
row=(row+n-1)%n;
col=(col+1)%n;
}
a[row][col]=j;
}//end for
for(row=0;row<n;row++){
for(col=0;col<n;col++) System.out.print(a[row][col]+" ");
System.out.println();
};
}
public static void main(String[]args){
int n=0;
TestMag tm=new TestMag();
String sn;
do{
System.out.print("请输入一个奇数:");
BufferedReader streami = new BufferedReader(
new InputStreamReader(System.in));
try {
sn=streami.readLine();
n=Integer.parseInt(sn);
}
catch (Exception ex) {
}
} while (n%2==0);
tm.Magic(n);
}
}