Java函数第二次调用时不起作用。

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

Java function not working when called twice

问题

public static int getMinor(int[][] mat, int r, int c) {
  int minor = 0;
  int[] min = new int[4];
  int x = 0;
  while (x <= 3) {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (i != r && j != c && mat[i][j] != 100) {
          min[x] = mat[i][j];
          x += 1;
          mat[i][j] = 100;
        }
      }
    }
  }
  minor = min[0] * min[3] - min[1] * min[2];
  return minor;
}

public static void main(String[] args) {
  int[][] mat = getMatrix();
  System.out.println(getMinor(mat, 0, 0));
  System.out.println(getMinor(mat, 1, 1));
}

for some reason when I call getMinor function again the code stops working.
It prints the minor the first time, but does not work when I call it again in the next line.
getMatrix function just gets the matrix.

英文:
public static int getMinor(int[][] mat, int r, int c) {
  int minor = 0;
  int[] min = new int[4];
  int x = 0;
  while (x &lt;= 3) {
    for (int i = 0; i &lt; 3; i++) {
      for (int j = 0; j &lt; 3; j++) {
        if (i != r &amp;&amp; j != c &amp;&amp; mat[i][j] != 100) {
          min[x] = mat[i][j];
          x += 1;
          mat[i][j] = 100;
        }
      }
    }
  }
  minor = min[0] * min[3] - min[1] * min[2];
  return minor;
}

public static void main(String[] args) {
  int[][] mat = getMatrix();
  System.out.println(getMinor(mat, 0, 0));
  System.out.println(getMinor(mat, 1, 1));
}

for some reason when I call getMinor function again the code stops working.
It prints the minor the first time but does not work when I call it again in the next line.
getMatrix function just gets the matrix.

答案1

得分: 0

你的代码存在潜在的无限循环可能性。(x <= 3) 可能永远不会变为 false,因此你的 while 循环会无限运行。虽然你没有给出输入矩阵,但我猜第一次调用 getMinor 返回并且 println 打印了返回结果,但然后执行第二次进入 getMinor,卡住了......永远不会出来,所以第二个 println 从未发生过,程序永远不会退出。

是的,就是这种情况。我随意构造了一个矩阵,我的运行结果打印了一个 0,然后在第二次调用 getMinor 时卡住了,不断在 while 循环中循环,条件 if (i != r && j != c && mat[i][j] != 100) 永远不会为 true,所以 x 永远不会改变。这是因为第二次操作的还是同一个矩阵,而已经设置为 100 的位置足以导致 while 循环永远不会退出。

如果第二次使用一个全新的矩阵,这种情况不太容易发生。这段代码,至少在我的示例矩阵中,会执行完:

int[][] mat = getMatrix();
System.out.println(getMinor(mat, 0, 0));
mat = getMatrix();
System.out.println(getMinor(mat, 1, 1));

你的代码可能还存在另一个问题(我说可能是因为也许你永远不会使用错误的参数,以至于不会发生这种情况),就是如果你传入大于矩阵宽度或高度边界的 rc 值,那么程序会崩溃并显示 index out of bounds error,因为 if 条件在一次矩阵遍历中可能会超过 4 次成功,所以在 min 数组中分配的空间不足,min[x] = mat[i][j]; 会出错。

英文:

Your code contains the potential for an infinite loop. It is possible for (x &lt;= 3) to never become false, and so for your while loop to run forever. You haven't given your input matrix, but I assume that what is happening is that the first call to getMinor returns and so println prints the returned result, but then the execution goes into getMinor the second time and gets stuck...never comes out, and so the second println never occurs and the program never exits.

Yes, that's what's going on. I just made up an arbitrary matrix, and my run prints a 0 and then locks up in the second call to getMinor, going around and around in the while loop forever with if (i != r &amp;&amp; j != c &amp;&amp; mat[i][j] != 100) never being true, and so x never changing. This occurs because you're operating on the same matrix the second time, and the locations already set to 100 are sufficient to cause the while loop to never exit.

If you start with a fresh matrix the second time, this doesn't so easily occur. This code, at least with my sample matrix, completes:

int[][] mat = getMatrix();
System.out.println(getMinor(mat, 0, 0));
mat = getMatrix();
System.out.println(getMinor(mat, 1, 1));

Another potential problem with your code (I say potential because maybe you will never use the wrong parameters so that this happens) is if you pass in r and c values that are larger than the w/h bounds of the matrix, then the program crashes with an index out of bounds error because the if clause succeeds more than 4 times in a run through the matrix, and so you haven't allocated enough slots in the min array, and min[x] = mat[i][j]; goes bang!.

答案2

得分: 0

  1. 你已经在 "getMinor" 中改变了 "mat" 的值,因此第二次调用与第一次调用的输入不同。(你可以阅读 这个链接,了解 Java 的 "按引用传递" 与 "按值传递")

  2. 在名为 "getXXX" 的函数内修改输入不是一个好的做法。

英文:
  1. You've changed the value of "mat" in "getMinor", so 2ed call dose not have same input as the 1st one . (You may read this to figure out java's "pass by reference" VS "pass by value")

  2. It's not a good practice to modify input within a function named as "getXXX"

huangapple
  • 本文由 发表于 2020年10月19日 10:48:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64420498.html
匿名

发表评论

匿名网友

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

确定