While 语句在 Repl.it (P5.js) 中使选项卡崩溃

While statement crashes tab in Repl.it (P5.js)

我有一个 27 号到期的项目,我遇到了一个问题。 Repl.it 每当我启动我的程序时崩溃。如果您查看我的代码,第 42 行:

//getting a number that isnt the players door or the prize door
  while ((randomDoor2 == prizeDoor) || (randomDoor2 == randomDoor)) {
    setTimeout(
      function(){ 
        randomDoor2 = Math.round(random(1,3));
      }, 
      10000);
  }

有一个 while 循环。将其注释掉可以使代码完美无延迟地工作。

我不知道我应该尝试做什么。

这可能不是我的时间,所以这是我的全部script.js:

var chance; // swap | dont swap
var prizeDoor;
var randomDoor;
var randomDoor2;
var randomDoor3;
var decide;

function setup() {
  chance = 50;
  createCanvas(1000,1000);
}

function draw() {
 //setting up round

  prizeDoor = Math.round(random(1,3));

  //choosing first door

  console.log("[1] [2] [3]");
  randomDoor = Math.round(random(1,3));

  //showing user the door AI picks
var chance = 50; // swap | dont swap
var prizeDoor;
var randomDoor;
var randomDoor2;
var randomDoor3;
var decide;

function setup() {
  chance = 50;
  createCanvas(1000,1000);
}

function draw() {
 //setting up round

  prizeDoor = Math.round(random(1,3));

  //choosing first door

  console.log("[1] [2] [3]");
  randomDoor = Math.round(random(1,3));

  //showing user the door AI picks

  if (randomDoor == 1) {
    console.log(" ^");
    console.log(" |");
  } else if (randomDoor == 2) {
    console.log("     ^");
    console.log("     |");
  } else {
    console.log("         ^");
    console.log("         |");
  }

  console.log("AI chooses door #" + randomDoor + ".");

  //revealing a door

  //getting a number that isnt the players door or the prize door
  while ((randomDoor2 == prizeDoor) || (randomDoor2 == randomDoor)) {
    setTimeout(
      function(){ 
        randomDoor2 = Math.round(random(1,3));
      }, 
      10000);
  }

  //showing this to the user
  console.log("");
  console.log("Door #" + randomDoor2 + " does not have the prize.");

  //having the computer make a desicion
  if (random(0,100) < chance) {
    decide = "swap doors.";
    while ((randomDoor3 !== randomDoor2) || (randomDoor3 !==  randomDoor)) {
      randomDoor3 = Math.round(random(1,3));
    }
  } else {
    decide = "keep the current door.";
  }

  //letting the user know of the computer's desicion
  console.log("");
  console.log("The AI chose to " + decide);

  // figuring out if the AI won
  if (randomDoor3 == prizeDoor || randomDoor == prizeDoor) {

    console.log("AI won!");

    if (decide == "swap doors.") {
      chance -= 5;
    } else {
      chance += 5;
    }
  } else {

    console.log("AI lost.");

    if (decide == "swap doors.") {
      chance += 5;
    } else {
      chance -= 5;
    }
  }
}

我想让 while 语句找到既不是所选门也不是有奖品的门,但它却崩溃了。

编辑我得到的代码工作确实改变了一些位,使它像 math.random 一样在我这边工作,但应该不难改回 Whosebug 的新手所以花了我一段时间找出粘贴的代码片段:p.

<script type="text/javascript">

var chance; // swap | dont swap
var prizeDoor;
var randomDoor;
var randomDoor2;
var randomDoor3;
var decide;
var chance = 50;
function setup() {
  createCanvas(1000,1000);
}

function draw() {
 //setting up round

  prizeDoor = Math.floor((Math.random() * 3) + 1);
  console.log(prizeDoor+" prizeDoor");

  //choosing first door
  console.log("[1] [2] [3]");
  randomDoor = Math.floor((Math.random() * 3) + 1);
  randomDoor2 = Math.floor((Math.random() * 3) + 1);
  randomDoor3 = Math.floor((Math.random() * 3) + 1);


  //showing user the door AI picks

  if (randomDoor == 1) {
    console.log(" ^");
    console.log(" |");
  } else if (randomDoor == 2) {
    console.log("     ^");
    console.log("     |");
  } else {
    console.log("         ^");
    console.log("         |");
  }



  console.log("AI chooses door #" + randomDoor + ".");


  //revealing a door

  //getting a number that isnt the players door or the prize door
  while ((randomDoor2 == prizeDoor) || (randomDoor2 == randomDoor)) {
   randomDoor2 = Math.floor((Math.random() * 3) + 1);
  }

  //showing this to the user
  console.log("");
  console.log("Door #" + randomDoor2 + " does not have the prize.");

  //having the computer make a desicion
  if (Math.floor((Math.random() * 100) + 1) < chance) {
    decide = "swap doors.";
    while ((randomDoor3 == randomDoor2) || (randomDoor3 == randomDoor)) {
      randomDoor3 = Math.floor((Math.random() * 3) + 1);
    }
  } else {
    randomDoor3 = randomDoor;
    decide = "keep the current door.";
  }

  //letting the user know of the computer's desicion
  console.log("");
  console.log("The AI chose to " + decide);

  // figuring out if the AI won
  if (randomDoor3 == prizeDoor) {

    console.log("AI won!");

    if (decide == "swap doors.") {
      chance -= 5;
    } else {
      chance += 5;
    }
  } else {

    console.log("AI lost.");

    if (decide == "swap doors.") {
      chance += 5;
    } else {
      chance -= 5;
    }
  }
}

draw();
</script>

您正在连续创建两个随机数 prizeDoor 和 randomDoor,因此使用 Math.random 连续两次点击生成相同的数字是很难发生的。

在你的 while 循环中,你期望你的新随机数现在与你之前的两个随机数匹配一个 hit/compilation 它们可以相同,也可以不相同,如果它们不相同,那么你的 while 循环就没用了它永远不会终止,

说 a=1 和 b=2

现在 期待 c==a && c==b 永远不会发生因为 a!=b;

在 while 循环中更改 c 不会有任何区别。 所以你的逻辑是完全错误的,让我们知道你想要达到的目标,也许我们可以帮助建立逻辑

let random = () => Math.random() * 3;
let prizeDoor = Math.round(random());
console.log('prizeDoor', prizeDoor)
let randomDoor = Math.round(random());
console.log('randomDoor', randomDoor)

random() 是 Math 对象的一个​​方法。 它通过用 Math.random().

替换所有 random() 方法调用对我有用

我明白了。其实很简单,只要换一些操作符就可以了。这是最后一段代码:

var chance = 50; // swap | dont swap
var prizeDoor;
var randomDoor;
var randomDoor2;
var randomDoor3;
var decide;

function setup() {
  chance = 50;
  createCanvas(1000,1000);
}

function draw() {
 //setting up round

  prizeDoor = Math.round(random(1,3));

  //choosing first door

  console.log("[1] [2] [3]");
  randomDoor = Math.round(random(1,3));

  //showing user the door AI picks

  if (randomDoor == 1) {
    console.log(" ^");
    console.log(" |");
  } else if (randomDoor == 2) {
    console.log("     ^");
    console.log("     |");
  } else {
    console.log("         ^");
    console.log("         |");
  }

  console.log("AI chooses door #" + randomDoor + ".");

  //revealing a door

  //getting a number that isnt the players door or the prize door
  while ((randomDoor2 == prizeDoor) || (randomDoor2 == randomDoor)) {
    setTimeout(
      function(){ 
        randomDoor2 = Math.round(random(1,3));
      }, 
      10000);
  }

  //showing this to the user
  console.log("");
  console.log("Door #" + randomDoor2 + " does not have the prize.");

  //having the computer make a desicion
  if (random(0,100) < chance) {
    decide = "swap doors.";
    while ((randomDoor3 == randomDoor2) || (randomDoor3 ==  randomDoor)) {
      randomDoor3 = Math.round(random(1,3));
    }
  } else {
    decide = "keep the current door.";
  }

  //letting the user know of the computer's desicion
  console.log("");
  console.log("The AI chose to " + decide);

  // figuring out if the AI won
  if (randomDoor3 == prizeDoor || randomDoor == prizeDoor) {

    console.log("AI won!");

    if (decide == "swap doors.") {
      chance -= 5;
    } else {
      chance += 5;
    }
  } else {

    console.log("AI lost.");

    if (decide == "swap doors.") {
      chance += 5;
    } else {
      chance -= 5;
    }
  }
}