我有一个项目将于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语句找到既不是选定的门,也不是带有奖品的门,但它崩溃了。
答案 0 :(得分:0)
编辑我让代码可以工作,但确实做了一些修改,使其可以像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>
答案 1 :(得分:0)
您要连续创建两个随机数prizeDoor和randomDoor,因此很难用Math.random在两个连续命中时生成相同的数字。
在while循环中,您希望您的新随机数现在与您以前的两个随机数匹配一次命中/编译,它们可以相同或不相同,如果它们不相同,那么使用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)
答案 2 :(得分:0)
random()是Math对象的一种方法。 它通过用Math.random()替换所有random()方法调用为我工作。
答案 3 :(得分:0)
我知道了。实际上很简单,只需更改一些运算符即可。这是最后的代码:
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;
}
}
}