【问题标题】:Don't understand why while runs itself again after ignoring a command不明白为什么 while 忽略命令后会再次运行
【发布时间】:2021-12-03 04:24:41
【问题描述】:

我在 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 循环时,不是运行命令 count++ 并将其值更改为 1,而是' t运行它,保持计数为0,然后在重新进入循环后更改它。

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

【问题讨论】:

  • 您如何确定您报告的行为?您能否提供示例输入,以及显示此操作的输出正在执行您所说的操作?另外,你知道i == 0; 什么都不做吗?
  • 如果johnnytrue,并且count 不等于A.length,则您有一个无限循环。
  • 你的代码格式真的很难阅读。我使用了 snipplet 和它的 format 函数让它可读和可运行
  • 进入for,进入while,返回for,进入while,再次循环进入while,退出。
  • 每次返回 for 循环时它都会重置.....设置断点并使用调试器遍历代码。

标签: javascript loops while-loop


【解决方案1】:

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

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

详情

第一次通过 while 循环时,B[i]2A[i]1tank += A[i] 使 tank 的值为 1。因此,b[i] &lt;= tankfalse,因此您将 johnny 设置为 false。这意味着在该迭代结束时,您的 while 条件 (... &amp;&amp; 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));

【讨论】:

  • 一开始我有点困惑,但现在很清楚了。谢谢
【解决方案2】:

目前尚不清楚您要做什么,但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));

【讨论】:

    猜你喜欢
    • 2015-03-16
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2019-05-28
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    相关资源
    最近更新 更多