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

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

int cannot be converted into int[][] error for 2D arrays

问题

嗨,我正在创建一个使用二维数组生成幻方的程序。在这个方法中,我应该将变量 size 初始化为 num,并且建立在方法外部创建的二维数组 ms。我得到了一个错误,提示 int 无法转换为 int[][]. 有人可以帮助我吗?

  1. class MagicSquare
  2. {
  3. private int size;
  4. private int[][] ms;
  5. public MagicSquare(int num) // 初始化 size 为 num 并建立 ms
  6. {
  7. size = num;
  8. ms = new int[size][size];
  9. }
  10. }
英文:

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?

  1. class MagicSquare
  2. {
  3. private int size;
  4. private int[][] ms;
  5. public MagicSquare(int num) // initialize size to num and establishes ms
  6. {
  7. num = size;
  8. ms = ms[num][num];
  9. }

答案1

得分: 1

你没有正确初始化数组
这是如何做的

ms = new int[num][num];

英文:

You didn't initialize the array correctly
This is how you would do it

  1. ms = new int[num][num];

答案2

得分: 1

在Java中,数组不能像那样初始化,请尝试:

  1. public MagicSquare(int num) // 初始化大小为num并建立ms
  2. {
  3. num = size;
  4. ms = new int[num][num];
  5. }
英文:

An array in Java cannot be initialised like that, try:

  1. public MagicSquare(int num) // initialize size to num and establishes ms
  2. {
  3. num = size;
  4. ms = new int[num][num];
  5. }

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:

确定