魔方Java程序
//凯文·克莱门特 //Week3A Magic Squares
大家好,正在对二维数组进行介绍性赋值。下面是我已经完成的代码,已经基本完成了。
我遇到的问题是我不完全确定如何打印出数组,以及如何使用测试方法让一切正常运行。我在 msq[order][order] = 1; 行处收到超出范围的错误;
如果我的问题格式错误,我仍然不习惯这个网站,我深表歉意。任何帮助都会很棒。谢谢!
import java.util.*;
class Magic
{
private int order;
int msq[ ][ ];
public Magic(int size)
{
if(size < 1 || size % 2 == 0)
{
System.out.print("Order out of range");
order = 3;
}
msq = new int[order][order];
Build();
Display();
}
public void Build()
{
int row = 0;
int col =0;
msq[order][order] = 1;
for(int k = 1; k <= order * order; k++)
{
msq[row][col] = k;
if(row == 0 && col == order -1)
row++;
else if(row == 0)
{
row = order - 1;
col++;
}
else if(msq[row - 1][col + 1] != 0)
row++;
else if(msq[row -1][col + 1] == 0)
{
row--;
col++;
}
if(col == order - 1)
{
col = 0;
row--;
}
}
}
public void Display()
{
for(int i = 0; i < order; i++)
{
for(int k = 0; k < order; k++)
{
System.out.println(msq[i][k] + " ");
}
}
}
}
//Kevin Clement
//Week3A Magic Squares
Hey all, doing an introductory assignment to 2dimensional arrays. Below is the code I have done which is pretty much done.
My problem I get is I'm not entirely sure how to print out the array, as well as getting everything to run right with a test method. I get an error out of bounds at the line msq[order][order] = 1;
I apologize if my formatting of question is wrong, still not used to this site. Any help would be great. Thanks!
import java.util.*;
class Magic
{
private int order;
int msq[ ][ ];
public Magic(int size)
{
if(size < 1 || size % 2 == 0)
{
System.out.print("Order out of range");
order = 3;
}
msq = new int[order][order];
Build();
Display();
}
public void Build()
{
int row = 0;
int col =0;
msq[order][order] = 1;
for(int k = 1; k <= order * order; k++)
{
msq[row][col] = k;
if(row == 0 && col == order -1)
row++;
else if(row == 0)
{
row = order - 1;
col++;
}
else if(msq[row - 1][col + 1] != 0)
row++;
else if(msq[row -1][col + 1] == 0)
{
row--;
col++;
}
if(col == order - 1)
{
col = 0;
row--;
}
}
}
public void Display()
{
for(int i = 0; i < order; i++)
{
for(int k = 0; k < order; k++)
{
System.out.println(msq[i][k] + " ");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果数组大小为n,则需要访问从0到n-1的元素。没有第 n 个索引。在您的情况下,没有 订单、订单 索引。它只是从0到order-1,并且是数组索引越界异常的原因。
If array size is n, then you need to access the elements from 0 to n-1. There is no nth index. In your case there is no order, order index. It is only from 0 to order-1 and is the reason for array index out of bounds exception.
构造函数中出现此条件的原因是什么?:
请注意,每当您使用不满足 if 子句的大小输入时,变量顺序都不会初始化并默认为 0。因此,二维数组的大小为零,并且抛出越界错误。如果您尝试使用 3 作为默认值,那么您需要将行:移到
if 块之前。
其他需要考虑的事项:
1.将订单变量设为最终变量,因为您不打算更改它。如果您这样做,Eclipse IDE 会警告您上述情况。
或者
2. 如果您要将 order 的值默认为 3,请按此进行初始化。
此外,您还可以考虑打印一条消息,说明当条件不满足时,订单默认为 3。
What is the reason for this condition in the constructor?:
Note that whenever you use a size input that doesn't satisfy the if clause, the variable order is not initialized and defaults to 0. As a result the 2d array has size zero and throws the out of bounds error. If you are trying to use 3 as a default value, then u want to move the line:
before the if block.
Other things to consider:
1. make the order variable final since u don't plan on changing it. Eclipse IDE would warn you about the situation described above if you do so.
or
2. If you are going to default to 3 for the value of order initialize it as such.
Also you might consider printing a message saying order defaults to three when the condition is not satisfied.
在 Java 中打印整数矩阵
Mahesh 回答了问题的第二部分。
To print a matrix of integers in Java
The second part of your question is answered by Mahesh.
msq[order][(order] = 1;
--> 这是一个语法错误。您有一个“(”。
你会得到结束绑定错误,因为数组从 0 开始而不是 1 (这是每个初学者都会犯的错误),因此将其更改为 msq[order-1][order-1] = 1;
上面的答案是打印数组的正确方法。
msq[order][(order] = 1;
--> here is a syntax error. You have an '('.
You get end of bound error because array start from 0 not 1 (which is a mistake every beginner makes) therefore change it to msq[order-1][order-1] = 1;
The answer above is the correct way to print out the array.