我正在使用的JavaScript。我试图将随机数设置为5个变量,它们不能相等。虽然循环检查多个变量是相等的
//set correct, dbcount is at 20ish now, will end up in the thousands later
correct = Math.floor(Math.random()*dbcount);
//intially set wrong variables
wrong1 = correct;
wrong2 = correct;
wrong3 = correct;
wrong4 = correct;
//set wrong ID not equal to each other or correct
while(wrong1 === correct && wrong2 === correct && wrong3 === correct && wrong4 === correct &&
wrong1 === wrong2 && wrong1 === wrong3 && wrong1 === wrong4 &&
wrong2 === wrong3 && wrong2 === wrong4 &&
wrong3 === wrong4){
wrong1 = Math.floor(Math.random()*dbcount);
wrong2 = Math.floor(Math.random()*dbcount);
wrong3 = Math.floor(Math.random()*dbcount);
wrong4 = Math.floor(Math.random()*dbcount);
}
每隔一段时间,会有一些相同的东西。我做错了吗?我应该使用嵌套if语句吗?需要的东西,将几乎做这样的事情:
while(a == b == c == x == y){
//then do stuff until a, b, c, x, y don't equal each other.
}
谢谢
你或许应该使用数组,而不是一堆的变量。 – elclanrs 2014-11-06 07:56:41
不==假设遵循传递属性? a == b,b == c然后a == c? – SMA 2014-11-06 07:59:00
只要在循环中生成随机数,并且每次生成上次迭代的随机数添加数时。这样,所有数字都不会相同。 – 2014-11-06 08:01:17