简体   繁体   English

||遇到麻烦 和&&

[英]Having trouble with || and &&

I am new to programming and wanted to make a dice rolling programm in Java for execise. 我是编程的新手,想在Java中制作骰子滚动程序以执行。 The code is the following: 代码如下:

import java.math.*;
public class Dices {
public static int dice1=0;
    public static int dice2=0;
    public static int x;
    public static void main(String args[]){
    do {
        x++;
        dice1=(int) (Math.random()*6+1);
        dice2=(int) (Math.random()*6+1);
        System.out.println(dice1+", "+dice2);
    } while(dice1 !=1 || dice2 !=1);
    System.out.println("Finalthrow: "+dice1+", "+dice2);
    System.out.println("Snake-Eyes after "+x+" tries.");
    }
}

This way it works fine, but in my opinion there is something wrong with the code. 这样可以正常工作,但是我认为代码有问题。 In the while condition should actually be. while条件实际上应该是。 But if I use && it stops as soon as it rolls a 1 on the first dice. 但是,如果我使用&&它将在第一个骰子上掷1时立即停止。 I thought && means "AND" and || 我以为&&意思是“ AND”和|| means "OR". 表示“或”。 So actually it should behave exactly the other way around, or am I misinterpreting something? 所以实际上它的行为应该完全相反,还是我误解了?

Some understanding about Morgan's laws could help here. 摩根法律的一些了解可能会有所帮助。 The law says (sorry for the weird syntax, but I think the message is clear) that : 法律说(很抱歉,语法很奇怪,但是我认为信息很清楚):

(!P) OR (!Q) == !(P AND Q)
(!P) AND (!Q) == !(P OR Q)

So when you use || 因此,当您使用|| (OR) in your condition (或)您的情况

while(dice1 !=1 || dice2 !=1)

is exactly the same as 与...完全相同

while(!(dice1 == 1 && dice2 == 1))

so it will looop until both dice are 1 . 因此它将循环直到两个骰子均为1

On the other hand, if you use && (AND) : 另一方面,如果您使用 && (AND)

while(dice1 !=1 && dice2 !=1)

it's the same as 和...一样

while(!(dice1 == 1 || dice2 == 1))

so it means that it will loop until one or two of the dice is/are 1 . 因此它意味着它将循环,直到骰子的一个或两个是/是1

&& means and
|| means or

So (dice1 != 1 || dice2 != 1) means continue the loop while dice1 is not 1 or dice 2 is not 1. 因此(dice1!= 1 || dice2!= 1)意味着在dice1不是1或dice 2不是1时继续循环。

So (dice1 != 1 && dice2 != 1) means continue the loop while dice1 is not 1 and dice 2 is not 1. 所以(dice1!= 1 && dice2!= 1)意味着当dice1不是1且dice 2不是1时继续循环。

The code is fine. 代码很好。 You want the loop to end when dice1 == 1 and dice2 == 1 . 您希望循环在dice1 == 1dice2 == 1 So it must loop until that is true, or until its opposite is false. 因此,它必须循​​环直到这是真的,或者直到相反。 The opposite of dice1 == 1 && dice2 == 1 is !(dice1 == 1 && dice2 == 1) which is equivalent to dice1 != 1 || dice2 != 1 dice1 == 1 && dice2 == 1!(dice1 == 1 && dice2 == 1) ,它等效于dice1 != 1 || dice2 != 1 dice1 != 1 || dice2 != 1 . dice1 != 1 || dice2 != 1

Think of it this way: if dice1 != 1 , keep looping. 这样想:如果dice1 != 1 ,继续循环。 Also, if dice2 != 1 , keep looping. 另外,如果dice2 != 1 ,请继续循环。 So if either is true, keep looping. 因此,如果任何一个都成立,请继续循环。 And to test if either is true, regardless of if both are true, use || 并使用||来测试任何一个是否为真,而不管两者是否为真。 .

The behavior is wright. 该行为是可笑的。 You're saying with dice1 !=1 && dice2 !=1 that repeat the loopuntil BOTH of the dices are NOT 1. But when one dice rolls a 1 the condition is false and the loop escapes. 您是在说dice1 !=1 && dice2 !=1重复循环直到两个骰子都不为1。但是,当一个骰子掷1时,条件为假,循环逃逸。 Try it with a truth table. 用真值表尝试一下。

Let's make a truth table. 让我们做一个真值表。

在此处输入图片说明

What can we conclude from this? 我们可以从中得出什么结论? If you want your loop to continue while either A or B are different from 1 (or, as De Morgan's laws say: until both of them are equal to 1 ) then you can narrow it down to these values: 如果你希望你的循环继续,而无论是 AB来自不同的1 (如德摩根定律说,或者: 直到两人都等于1 ),那么你可以缩小范围,这些值:

在此处输入图片说明

Since we need to find the operator that allows us to continue the loop (aka: the condition is true ), we take the column that returns true for all 3 different kinds of inputs, which is A || B 由于我们需要找到允许我们继续循环的运算符(又称:condition为true ),因此我们采用对所有3种不同类型的输入均返回true的列,即A || B A || B . A || B

Note that A && B and A || B 请注意, A && BA || B A || B refer to the result of A != 1 and B != 1 , not the actual input. A || B是指A != 1B != 1 ,而不是实际输入。

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

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