无法将 int 转换为 int[][],针对二维数组的错误。

huangapple go评论108阅读模式
英文:

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];
}

huangapple
  • 本文由 发表于 2020年10月27日 23:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64557820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定