Java 如何仅使用一维数组生成幻方?
我的任务是提示用户给出幻方的顺序(3阶幻方是3x3矩阵),然后在不使用二维数组的情况下生成该顺序的幻方 以下是我所知道和理解的:Java 如何仅使用一维数组生成幻方?,java,arrays,magic-square,Java,Arrays,Magic Square,我的任务是提示用户给出幻方的顺序(3阶幻方是3x3矩阵),然后在不使用二维数组的情况下生成该顺序的幻方 以下是我所知道和理解的: 该数组将具有有序平方元素 打印表格时,数字在数组中的位置为[order×row+col] 表上数组位置的索引为(索引/顺序,索引%顺序) 以下是我得到的信息,但我不知道如何正确实施: 行=订单− 1,col=order/2,i=1 重复以下步骤,直到i=顺序^2+1: (a) 魔术[索引]=i (b) 将行和列增加1。也就是说,行=(行+1)mod顺序和列=(列+
- 该数组将具有有序平方元素
- 打印表格时,数字在数组中的位置为[order×row+col]
- 表上数组位置的索引为(索引/顺序,索引%顺序)
i. row = (row + order − 2) % order
ii. col = (col + order − 1) % order
(d) 增量i乘以1package 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);
}
}
package magicsquarecreator;
导入java.io.IOException;
导入java.util.Scanner;
公共类MagicSquareDemo
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(系统输入);
字符串选项;
做
{
System.out.println();
System.out.println(“幻方应用程序”);
System.out.println(“=========================================================================”);
System.out.println(“生成幻方[1]”;
System.out.println(“幻方测试”……[2]”;
System.out.println(“退出程序[0]”;
System.out.println();
系统输出打印(“选择一个选项->”;
option=keyIn.next();
System.out.println();
开关(选件)
{
案例“1”:
尝试
{
System.out.println(“输入幻方的尺寸->”;
int order=keyIn.nextInt();
如果(顺序<1)
{
System.out.println(“订单必须是正数和奇数”);
}
否则如果(订单%2==0)
{
System.out.println(“程序只能生成幻方”
+“\n维度为正奇数。”);
}
否则如果(订单%2!=0)
{
int i=1,j=1;
int行=顺序-1;
int col=订单/2;
int[]魔术=新的int[order*order];
因为(i=1;i老实说,我也没有真正理解TAK,因为它是从美国写的。如果我按照我在那里的理解去做,它就不起作用了。
但我看了看它应该如何工作,最后我得到了它。
以下是else if(订单%2!=0)
条件作为方法的部分(我测试了它,它现在正在工作):
publicstaticvoidmagicsqare(整数顺序){
系统输出打印项次(订单);
int行=顺序-1;
int col=订单/2;
int[]魔术=新的int[order*order];
整数指数;
索引=(行)*顺序+列;
魔术[指数]=1;
对于(int i=2;i在标题中,你问的是一维数组,但在问题的主体中,你说任务是使用二维数组。是哪一个?如果你基于两个轴计算偏移量,例如row*row_width+column,一维数组可以用作二维数组。毕竟,这就是语言在内部实现它的方式,假设数组是连续存储的,而不是作为子列表的列表存储的。aahh我上次编辑错误,请不要确认it@keshlam我不确定我是否理解这是如何工作的。你能进一步解释一下吗?我不确定我是否理解你的编辑。如果你不介意看一看的话,这里有一个指向确切作业的链接。
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));
}