英文:
int cannot be converted into int[][] error for 2D arrays
问题
嗨,我正在创建一个使用二维数组生成幻方的程序。在这个方法中,我应该将变量 size 初始化为 num,并且建立在方法外部创建的二维数组 ms。我得到了一个错误,提示 int 无法转换为 int[][]. 有人可以帮助我吗?
class MagicSquare
{
private int size;
private int[][] ms;
public MagicSquare(int num) // 初始化 size 为 num 并建立 ms
{
size = num;
ms = new int[size][size];
}
}
英文:
Hi I am creating a program that generates magic squares using 2D arrays. In this method I am supposed initialize the variable size to num and establish the 2D array ms which was created outside the method. I am getting an error that says int cannot be converted into int[][]. Can someone help me?
class MagicSquare
{
private int size;
private int[][] ms;
public MagicSquare(int num) // initialize size to num and establishes ms
{
num = size;
ms = ms[num][num];
}
答案1
得分: 1
你没有正确初始化数组
这是如何做的
ms = new int[num][num];
英文:
You didn't initialize the array correctly
This is how you would do it
ms = new int[num][num];
答案2
得分: 1
在Java中,数组不能像那样初始化,请尝试:
public MagicSquare(int num) // 初始化大小为num并建立ms
{
num = size;
ms = new int[num][num];
}
英文:
An array in Java cannot be initialised like that, try:
public MagicSquare(int num) // initialize size to num and establishes ms
{
num = size;
ms = new int[num][num];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论