首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Repl.it (P5.js)中的While语句崩溃选项卡

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

Stack Overflow用户
提问于 2019-03-23 23:43:58
回答 4查看 104关注 0票数 0

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

代码语言:javascript
复制
//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循环。注释掉它可以使代码完美地工作,没有延迟。

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

它可能不是我的while,所以这是我的整个script.js:

代码语言:javascript
复制
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语句找到的门既不是所选的门,也不是有奖品的门,但它却崩溃了。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-03-24 22:59:18

我想通了。这实际上非常简单,只需更改一些运算符即可。下面是最后一段代码:

代码语言:javascript
复制
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;
    }
  }
}
票数 0
EN

Stack Overflow用户

发布于 2019-03-24 00:09:55

编辑我让代码工作了,做了一些改动,让它像math.random一样在我这端工作,但是应该不难改回来,也是stackoverflow的新手,所以我花了一段时间来弄清楚代码片段粘贴:p。

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2019-03-24 00:16:17

您将连续创建两个随机数prizeDoor & randomDoor,因此很难在两个连续的命中中使用Math.random生成相同的数。

在你的while循环中,你期望你的新的随机数与你之前的两个随机数匹配,因为一次点击/编译,它们可以是相同的,也可以不是,如果它们不相同,那么你的while循环将永远不会终止,这是没有用的,

比如a=1和b=2

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

在while循环中更改c不会有任何影响。所以你的逻辑是完全错误的,让我们知道你试图实现什么,然后我们可以帮助建立逻辑

代码语言:javascript
复制
let random = () => Math.random() * 3;
let prizeDoor = Math.round(random());
console.log('prizeDoor', prizeDoor)
let randomDoor = Math.round(random());
console.log('randomDoor', randomDoor)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55315498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档