我的 while 循环中的条件似乎不正确?
the condition in my while loop doesn't seem right?
尝试编写一个程序,投掷 3 次骰子,一旦连续获得 3 个 6,它就会打印出尝试了多少次。代码末尾有问题,|| 之间的差异而&&,好像是相反的,看看...
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.
}
}
你的第一个条件是:
总和不是 18,所以它确实应该可以正常工作。
第二个条件是:
至少一个die不是6个,所以应该也能正常工作。
你的最后一个条件:
没有骰子是6,所以即使一个骰子掷出6,它也会坏掉,所以它不起作用是有道理的。
更新:
AND (&&) 表示参数的两边都必须为真,输出才为真,
OR (||) 意味着参数的一侧需要为真才能使结果为真。
您需要重温boolean algebra的基本操作。
它应该是这样工作的。
这个:
do{
}while(array[0] !=6 && array[1] != 6 && array[2] != 6)
意味着只要条件为真,循环就会继续。换句话说——如果任一部分被评估为假,那么循环将停止。所以如果 array[0]==6
那么循环就会中断。
while (array[0] !=6 || array[1] != 6 || array[2] != 6);
这很好用,因为它意味着:"while one of the three attempts is not 6"
所以退出条件是:"all the die are 6"
while (array[0] !=6 && array[1] != 6 && array[2] != 6);
这不起作用,因为它意味着:"while neither of the three attempts is 6"
所以退出条件是:"at least one die is 6"
while (array[0]+array[1]+array[2] != 18);
这很好用,因为它意味着:"the die result sum is 18"
所以退出条件是:"all the die are 6"(因为这是求和 18 的唯一方法)
我相信您的困惑来自 De Morgan's laws of negation。基本上,当您对一组条件进行否定时,您应该将 &&
更改为 ||
,反之亦然,当您对单个条件进行否定时。
或者简单地说:
!(A && B) == !A || !B
!(A || B) == !A && !B
在你的情况下,你想这样做:
!(array[0] == 6 && array[1] == 6 && array[2] == 6)
又名。 "while it's not true that the first is 6 AND the second is 6 AND the third is six"
由于上面的规律,为了把!
带进去,需要把&&
换成||
,导致
!(array[0] == 6) || !(array[1] == 6) || !(array[2] == 6)
简化为
array[0] != 6 || array[1] != 6 || array[2] != 6
一般:
do{
something....
}while(some condition is true);
如果要用'&&'运算符表示
do{
something....
}
while (!(array[0] == 6 && array[1] == 6 && array[2] == 6));
意思是
do {
something...
}while(the expression every element in the array is equal to 6 is false);
尝试编写一个程序,投掷 3 次骰子,一旦连续获得 3 个 6,它就会打印出尝试了多少次。代码末尾有问题,|| 之间的差异而&&,好像是相反的,看看...
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.
}
}
你的第一个条件是: 总和不是 18,所以它确实应该可以正常工作。
第二个条件是: 至少一个die不是6个,所以应该也能正常工作。
你的最后一个条件: 没有骰子是6,所以即使一个骰子掷出6,它也会坏掉,所以它不起作用是有道理的。
更新: AND (&&) 表示参数的两边都必须为真,输出才为真, OR (||) 意味着参数的一侧需要为真才能使结果为真。
您需要重温boolean algebra的基本操作。
它应该是这样工作的。 这个:
do{
}while(array[0] !=6 && array[1] != 6 && array[2] != 6)
意味着只要条件为真,循环就会继续。换句话说——如果任一部分被评估为假,那么循环将停止。所以如果 array[0]==6
那么循环就会中断。
while (array[0] !=6 || array[1] != 6 || array[2] != 6);
这很好用,因为它意味着:"while one of the three attempts is not 6"
所以退出条件是:"all the die are 6"
while (array[0] !=6 && array[1] != 6 && array[2] != 6);
这不起作用,因为它意味着:"while neither of the three attempts is 6"
所以退出条件是:"at least one die is 6"
while (array[0]+array[1]+array[2] != 18);
这很好用,因为它意味着:"the die result sum is 18"
所以退出条件是:"all the die are 6"(因为这是求和 18 的唯一方法)
我相信您的困惑来自 De Morgan's laws of negation。基本上,当您对一组条件进行否定时,您应该将 &&
更改为 ||
,反之亦然,当您对单个条件进行否定时。
或者简单地说:
!(A && B) == !A || !B
!(A || B) == !A && !B
在你的情况下,你想这样做:
!(array[0] == 6 && array[1] == 6 && array[2] == 6)
又名。 "while it's not true that the first is 6 AND the second is 6 AND the third is six"
由于上面的规律,为了把!
带进去,需要把&&
换成||
,导致
!(array[0] == 6) || !(array[1] == 6) || !(array[2] == 6)
简化为
array[0] != 6 || array[1] != 6 || array[2] != 6
一般:
do{
something....
}while(some condition is true);
如果要用'&&'运算符表示
do{
something....
}
while (!(array[0] == 6 && array[1] == 6 && array[2] == 6));
意思是
do {
something...
}while(the expression every element in the array is equal to 6 is false);