简体   繁体   English

虽然语句崩溃 Repl.it (P5.js) 中的选项卡

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

I have a project due on the 27th, and I have encountered a problem.我有一个27号到期的项目,遇到了一个问题。 Repl.it crashes whenever I boot up my program.每当我启动我的程序时,Repl.it 就会崩溃。 If you look at my code, on line 42:如果您查看我的代码,在第 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);
  }

there is a while loop.有一个while循环。 Commenting that out makes the code work perfectly with no lag.注释掉可以使代码完美无延迟地工作。

I have no idea what I should try and do.我不知道我应该尝试做什么。

It might be something other than my while, so here is my entire script.js:这可能不是我的时间,所以这是我的整个 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;
    }
  }
}

I wanted the while statement to find the door that is not the chosen door nor the door with the prize, but instead it crashes.我希望 while 语句可以找到既不是所选门也不是带有奖品的门的门,而是它崩溃了。

Edit i got the code to work did change up a few bits to make it work on my end like the math.random but shouldn't be hard to change back also new to stackoverflow so took me a while to figure out the code snippet pasting :p.编辑我让代码工作确实改变了一些位,使其像 math.random 一样在我的一端工作,但不应该很难改回来也是 stackoverflow 的新手,所以我花了一些时间来弄清楚代码片段粘贴: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>

you are creating two random numbers consecutively prizeDoor & randomDoor, so same numbers to be generated using Math.random on two consecutive hits is quite hard to happen.您正在连续创建两个随机数 PrizeDoor 和 randomDoor,因此在两次连续命中时使用 Math.random 生成相同的数字是很难发生的。

In your while loop you are expecting your new random number to match your both previous random numbers now for one hit/compilation they can be same or cannot be if they are not same its useless to have your while loop it will will never terminate,在您的 while 循环中,您希望您的新随机数现在与您之前的两个随机数匹配,以便进行一次点击/编译,它们可以相同,也可以不同,如果它们不相同,那么您的 while 循环将永远不会终止,

say a=1 and b=2说 a=1 和 b=2

now expecting c==a && c==b will never happen cuz a!=b;现在期待 c==a && c==b 永远不会发生,因为 a!=b;

changing c in while loop will make no difference.在 while 循环中更改 c 将没有任何区别。 so your logic is completely wrong, let us know what you are trying to achieve maybe then we can help to build up the logic所以你的逻辑是完全错误的,让我们知道你想要实现的目标,也许我们可以帮助建立逻辑

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

random() is a method of Math object. random() 是 Math 对象的一种方法。 It worked for me by replacing all the random() method calls with Math.random().它通过用 Math.random() 替换所有 random() 方法调用对我有用。

I figured it out.我想到了。 It was actually pretty easy, just change some operators.其实很简单,只需要改变一些操作符即可。 Here is the final piece of code:这是最后一段代码:

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;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM