JavaScript逻辑运算符-和vs或 [英] Javascript logical operators - and vs. or

查看:119
本文介绍了JavaScript逻辑运算符-和vs或的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总体而言,我对Javascript和程序设计还比较陌生.今天,在编写一个简单的三骰子掷骰器模拟器时,我遇到了一个我解决的问题,但我仍然不明白.这是我的代码...

I'm relatively new to Javascript and programming in general. Today while writing a simple triple dice roll simulator I struck a problem that I worked around but which I still don't understand. Here's my code...

//                      ROLLING TRIPLES

var diceSides = 6;
var diceOne = Math.floor(Math.random() * diceSides + 1);
var diceTwo = Math.floor(Math.random() * diceSides + 1);
var diceThree = Math.floor(Math.random() * diceSides + 1);
var rolls = 3;

while ((diceOne !== diceTwo) || (diceTwo !== diceThree)) {
    console.log("Dice 1: " + diceOne);
    diceOne = Math.floor(Math.random() * diceSides + 1);
    console.log("Dice 2: " + diceTwo);
    diceTwo = Math.floor(Math.random() * diceSides + 1);
    console.log("Dice 3: " + diceThree);
    diceThree = Math.floor(Math.random() * diceSides + 1);
    console.log("Rolling again");
    rolls += 3;
}
console.log("Dice 1: " + diceOne);
console.log("Dice 2: " + diceTwo);
console.log("Dice 3: " + diceThree);
console.log("Rolled a triple!!!");

console.log(rolls);

问题是"while"条件: while(((diceOne!== diceTwo)||(diceTwo!== diceThree))

The problem is the 'while' condition: while ((diceOne !== diceTwo) || (diceTwo !== diceThree))

使用"||"运算符,该程序将按预期运行,并在diceOne = diceTwo = diceThree时退出"while"循环,即,您滚动三元组.但是,这对我来说没有意义...使用"||"运算符似乎表明"while"循环将完成,条件被评估为false,只有两个骰子相等...

Using an '||' operator, the program functions as intended, and breaks out of the 'while' loop when diceOne = diceTwo = diceThree i.e. you roll a triple. This doesn't make sense to me however... Using an '||' operator it would appear that the 'while' loop would finish, the condition having evaluated to false with only TWO of the die being equal...

例如它会返回如下结果:

e.g. it would return a result like:

Dice 1: 4
Dice 2: 4
Dice 3: 6
Rolled a triple!!!

因为在这种情况下,即使diceTwo不等于diceThree,diceOne也等于diceTwo.在这种情况下,请使用'||'运算符,我希望'while'循环会停止,因为它看起来已经满足了条件...但是没有,它将返回:

Because in this case, diceOne DOES equal diceTwo, even though diceTwo does not equal diceThree. In this case, using an '||' operator, I would expect the 'while' loop to stop because it appears the condition has been met... But it doesn't, it would return:

Dice 1: 4
Dice 2: 4
Dice 3: 6
Rolling again

...我期望使用'&&::运算符.除非带有&&"运算符,代码返回我期望的'||'运算符:

...What I would expect with an '&&: operator. Except with an '&&' operator the code returns what I would expect with an '||' operator:

Dice 1: 4
Dice 2: 4
Dice 3: 6
Rolled a triple!!!

即使尚未滚动三元组,代码也将完成.这就是在我的脑海中发出&&"的声音的方式.运算符...

The code finishes, even though a triple hasn't been rolled. This is the way it sounds in my head with an '&&' operator...

如果diceOne和diceTwo与diceThree相等,则说明您掷出了三元组."

"If diceOne and diceTwo AND diceThree are equal, you've rolled a triple."

带有'||'运算符...

with an '||' operator...

如果diceOne和diceTwo相等,或者diceTwo和diceThree相等,则说明您已掷出三元组."

"If diceOne and diceTwo are equal, OR diceTwo and diceThree are equal, you've rolled a triple."

您显然没有,因为三个骰子中只有两个是相同的.

which you clearly haven't because only two of the three die are the same.

我知道我一直在不断……对我来说有点难以解释.可能有一个非常简单的解释,但这确实困扰了我!

I know I'm going on and on and on... It's kinda hard for me to explain. There's probably a really simple explanation but it's really bugging me!

附带说明:是否有任何快捷方式可用于多次生成随机数,而无需键入Math.floor(Math.random ..... 我不能将其分配给变量并输入该变量,因为它会一次生成一个随机数,并且每次遇到该变量时都会使用该数字.有没有更有效的方法?

On a side note: is there any shortcut I can use to generate a random number numerous times without having to type Math.floor(Math.random..... I can't assign it to a variable and enter the variable because it generates a random number once and uses that number every time it encounters the variable. Is there a more efficient way to do this??

欢呼

推荐答案

您对逻辑运算符的理解是正确的,您对while的工作方式感到困惑.当满足条件时,它将继续循环,而当条件失败时,它将停止.

Your understanding of the logical operators is correct, you're confused about how while works. When the condition is met it keeps looping, it stops when the condition fails.

因此,当只有两个骰子相等时,!=条件之一将成立,因此||条件也成立,因此它将继续循环.当三个骰子都相等时,两个!=条件都将为false,并且false || false也是false,因此循环停止.

So when only two of the dice are equal, one of the != conditions will be true, so the || condition is also true, so it continues to loop. When all three dice are equal, both != conditions will be false, and false || false is also false, so the loop stops.

某些语言具有until语句,与while类似,但是会反转条件,以便当条件为true时循环就停止.可以按照您的描述进行操作.

Some languages have an until statement, which is like while but inverts the condition so that the loop stops when the condition is true. That would work the way you were describing.

这篇关于JavaScript逻辑运算符-和vs或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆