简体   繁体   English

我的while循环中的条件似乎不正确吗?

[英]the condition in my while loop doesn't seem right?

trying to write a program that casts a die 3 times and once you get 3 6s in a row it prints out how many tries it took. 尝试编写一个将骰子投放3次的程序,一旦连续获得3个6s,它就会打印出需要尝试的次数。 having a problem at the end of the code, the diff between || 在代码末尾有问题,||之间的差异 and &&, seems like it's the opposite, have a look... 和&&似乎相反,看看...


package javaapplication12;
import java.util.*;

public class JavaApplication12 {
public static void main(String[] args) {
  Random rand = new Random();
  // this should try to cast a die 3 times and keep doing that until we get 3 6s in a row and counter 
  // how many tries it takes to get that.
  int array[] = new int[3]; // creating 3 places for castin our die 3 times in a row
  int counter1 = 0; // creating a counter to track each time we try to cast 3 die ( 3 cast = 1 try)
  do{

      counter1++; 
      System.out.println("try " + counter1); // prints out counter in the beginning of every try.

      for (int counter = 0; counter < array.length; counter++ ){
          array[counter]=(rand.nextInt(6)+1);  // a loop that fills the array with random numbers from 1-6
      }
      for(int x: array)
          {System.out.println(x);} // this is for our own to check if we have 3 6s in a row,
                                   // prints out all the numbers in out array
  }

  //so this is the I'veusing part, i've written 3 scenarios that can be written for the condtion in our
  // do- while loop...

  while (array[0]+array[1]+array[2] != 18); // this works just fine.

  while (array[0] !=6 || array[1] != 6 || array[2] != 6); // imo this should not work but surprisingly it does

  while (array[0] !=6 && array[1] != 6 && array[2] != 6); // this should work, but it doesnt.


  } 
}

I believe your confusion comes forth from De Morgan's laws of negation . 我相信您的困惑来自De Morgan的定律 Basically, when you have a negation of a group of conditions, you should change && by || 基本上,当您否定一组条件时,应通过||更改&& and vice versa when you negate the individual conditions. 否定各个条件时,反之亦然。

Or simply put: 或简单地说:

!(A && B) == !A || !B
!(A || B) == !A && !B

In you case, you want to do this: 在这种情况下,您想这样做:

!(array[0] == 6 && array[1] == 6 && array[2] == 6)

aka. 又名。 "while it's not true that the first is 6 AND the second is 6 AND the third is six" “虽然第一个是6,第二个是6,第三个是6,这不是真的”

Due to the above law, in order to bring the ! 由于上述法律,为了带来! inside, you need to change && for || 在里面,您需要更改&&|| , resulting in , 导致

!(array[0] == 6) || !(array[1] == 6) || !(array[2] == 6)

Simplified to 简化为

array[0] != 6 || array[1] != 6 || array[2] != 6

Your first condition is: The sum is not 18, so indeed it should work correctly. 您的第一个条件是:总和不是18,因此确实应该正确运行。

The second condition is: At least one die is not 6, so it should also work correctly. 第二个条件是:至少一个骰子不是6,所以它也应该正常工作。

Your last condition: No die is 6, so it will break even if a single die rolls 6, so it makes sense that it does not work. 您的最后一个条件:没有骰子是6,所以即使单个骰子滚动6,它也将断裂,因此这是行不通的。

UPDATE: The AND (&&) means that both sides of the arguments must be true for the output to be true, the OR (||) means that one side of the arguments needs to be true for the outcome to be true. 更新:AND(&&)表示参数的两面都必须为真,才能使输出为真; OR(||)表示参数的一面必须为真,以使结果为真。

You need to revisit the basic operations of boolean algebra . 您需要重新研究布尔代数的基本运算。

That is how it was supposed to work. 那应该是这样的。 This one: 这个:

do{
}while(array[0] !=6 && array[1] != 6 && array[2] != 6)

mean that as long as the condition is true the loop will continue. 意味着只要条件为真,循环就会继续。 Putting it the other way - if either part is evaluated to false then the loop will stop. 换句话说,如果将任一部分评估为false,则循环将停止。 So if array[0]==6 then the loop will break. 因此,如果array[0]==6则循环将中断。

 while (array[0] !=6 || array[1] != 6 || array[2] != 6);

This works fine because it means: "while one of the three attempts is not 6" 这很好,因为它的意思是:“虽然三个尝试之一不是6”

so the exit condition is: "all the die are 6" 因此退出条件为:“所有骰子均为6”

while (array[0] !=6 && array[1] != 6 && array[2] != 6);

This doesn't works because it means: "while neither of the three attempts is 6" 这是行不通的,因为它的意思是:“虽然三个尝试都不是6”

so the exit condition is: "at least one die is 6" 因此退出条件为:“至少一个骰子为6”

 while (array[0]+array[1]+array[2] != 18);

This works fine because it means: "the die result sum is 18" 之所以可以正常工作,是因为它意味着:“模具结果总和为18”

so the exit condition is: "all the die are 6" (because it's the only way to sum 18) 因此退出条件为:“所有骰子均为6”(因为这是加总18的唯一方法)

Generally: 通常:

do{
    something....
}while(some condition is true);

If you want to express it with the '&&' operator 如果要使用“ &&”运算符表示

do{
    something....
}
while (!(array[0] == 6 && array[1] == 6 && array[2] == 6));

meaning 含义

  do { 
     something...
  }while(the expression every element in the array is equal to 6 is false);

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

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