不明白为什么 while 在忽略命令后再次运行

Don't understand why while runs itself again after ignoring a command

我在 JS 中有这段代码

//param A : array of integers
//param B : array of integers
//return an integer
function doesitwork(A, B) {
  if (A.length != B.length) {
    return -1;
  }
  var i = 0;
  var tank;
  var count;
  var johnny = true;
  for (var j = 0; j < A.length; j++) {
    count = 0;
    tank = 0;
    i = j;
    johnny = true;
    while (count < A.length && johnny == true) {
      console.log(i);
      console.log(A.length);
      console.log(count);
      console.log(johnny);
      tank += A[i];
      if (B[i] <= tank)
        tank -= B[i];
      else {
        johnny = false;
      }
      count++;
      i++;
      if (i == A.length)
        i == 0;

    }
    while (johnny == true) {
      if (count == A.length)
        return j;
    }
  }
  return -1;
}

var A = [1, 2];
var B = [2, 1];
console.log(doesitwork(A, B));

我不明白的是当我运行这个程序的时候,一开始count = 0,但是当我第一次进入while循环时,而不是运行ning命令count++ 并将其值更改为 1,它不会 运行 它,保持计数为 0,然后在重新进入循环后更改它。

TLDR:While 循环 运行ning 两次,其中一个变量保持相同的值,而不是像循环中的一行建议的那样更改它。

不清楚您要做什么,但是 while 循环的条件不会始终为真,这使得 count= 0for 循环中。所以,你需要输入 var count =0;

//param A : array of integers
//param B : array of integers
//return an integer
function doesitwork(A, B) {
  if (A.length != B.length) {
    return -1;
  }
  var i = 0;
  
  var tank;
  var count=0;
  var johnny = true;
  for (var j = 0; j < A.length; j++) {
   
    tank = 0;
    i = j;
    johnny = true;
    while (count < A.length && johnny == true) {
      console.log(i);
      console.log(A.length);
      console.log(count);
      console.log(johnny);
      tank += A[i];
      if (B[i] <= tank)
        tank -= B[i];
      else {
        johnny = false;
      }
      count++;
      i++;
      if (i == A.length)
        i == 0;

    }
    while (johnny == true) {
      if (count == A.length)
        return j;
    }
  }
  return -1;
}

var A = [1, 2];
var B = [2, 1];
console.log(doesitwork(A, B));

TLDR: While loop is running twice with one of the variables staying with the same value instead of changing it as one of the lines in the loop suggests.

您假设 count 从未递增,因为您假设 while 循环第二次继续。实际上,while 循环在单次迭代后退出,而 for 循环将 count 重置为 0。然后再次进入 while 循环,但 j.

的值不同

详情

第一次通过 while 循环时,B[i]2A[i]1tank += A[i] 使 tank 的值为 1。因此,b[i] <= tankfalse,因此您将 johnny 设置为 false。这意味着在该迭代结束时,您的 while 条件 (... && johnny == true) 为 false,因此您不会第二次继续执行 while 循环。

相反,控制移至 for 循环,它将 count 重置为 0

以后要调试此类问题,学习使用调试器会有所帮助:您可以在需要的地方放置一个断点,然后查看控制流程以及变量在您逐步执行时如何变化。如果您发现这不可能,您可能需要更加详细地记录日志,如下所示。

//param A : array of integers
//param B : array of integers
//return an integer
function doesitwork(A, B) {
  if (A.length != B.length) {
    return -1;
  }
  var i = 0;
  var tank;
  var count;
  var johnny = true;
  for (var j = 0; j < A.length; j++) {
    console.log("entering for loop");
    count = 0;
    tank = 0;
    i = j;
    johnny = true;
    while (count < A.length && johnny == true) {
      console.log("entering while loop");
      console.log("i", i);
      console.log("j", j);
      console.log("A.length", A.length);
      console.log("count", count);
      console.log("johnny", johnny);
      tank += A[i];
      if (B[i] <= tank)
        tank -= B[i];
      else {
        console.log(B[i], tank, "setting johnny to false");
        johnny = false;
      }
      count++;
      i++;
      if (i == A.length)
        i == 0;

    }
    while (johnny == true) {
      if (count == A.length)
        return j;
    }
  }
  return -1;
}

var A = [1, 2];
var B = [2, 1];
console.log(doesitwork(A, B));