【问题标题】:the condition in my while loop doesn't seem right?我的 while 循环中的条件似乎不正确?
【发布时间】:2019-07-15 08:07:17
【问题描述】:

尝试编写一个掷骰子 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.


  } 
}

【问题讨论】:

  • 一做,就可以一会。所以你唯一的while条件应该是while (array[0] !=6 &amp;&amp; array[1] != 6 &amp;&amp; array[2] != 6);
  • 我知道我暂时只写了 3 个场景,是的,一个应该可以工作,但它没有。奇怪的是,如果我最后一次使用 &&,它会在骰子值达到 6 时结束循环,而不是全部。以及带有 || 的第二个 while 选项工作得很好,当我们连续获得 3 个 6 时停止循环,这就是为什么这很奇怪......它应该是相反的。
  • 您使用的“die”标签与掷骰子无关,而是与使用名为 die() 的函数有关(顺便说一句,该函数在 java 中不存在)。请删除它。

标签: java die


【解决方案1】:

我相信您的困惑来自De Morgan's laws of negation。基本上,当您对一组条件进行否定时,您应该将&amp;&amp; 更改为||,反之亦然。

或者简单地说:

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

在你的情况下,你想这样做:

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

又名。 “虽然第一个是 6,第二个是 6,第三个是 6,这不是真的”

由于上面的规律,为了把!带进去,需要把&amp;&amp;改成||,导致

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

简化为

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

【讨论】:

    【解决方案2】:

    您的第一个条件是: 总和不是 18,所以它确实应该可以正常工作。

    第二个条件是: 至少一个骰子不是 6,所以它应该也能正常工作。

    你的最后一个条件: 没有骰子是 6,所以即使一个骰子掷出 6 也会碎,所以它不起作用是有道理的。

    更新: AND (&&) 意味着参数的两边都必须为真,输出才会为真, OR (||) 意味着参数的一侧需要为真才能使结果为真。

    你需要重温boolean algebra的基本操作。

    【讨论】:

      【解决方案3】:

      它应该是这样工作的。 这个:

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

      意味着只要条件为真,循环就会继续。换句话说——如果任一部分被评估为假,那么循环将停止。所以如果array[0]==6 那么循环就会中断。

      【讨论】:

        【解决方案4】:
         while (array[0] !=6 || array[1] != 6 || array[2] != 6);
        

        这很好用,因为它意味着:“虽然三个尝试之一不是 6”

        所以退出条件是:“所有骰子都是6”

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

        这不起作用,因为它的意思是:“虽然三个尝试都不是 6”

        所以退出条件是:“至少一个骰子是6”

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

        这很好用,因为它的意思是:“骰子结果总和是 18”

        所以退出条件是:“所有骰子都是 6”(因为这是求和 18 的唯一方法)

        【讨论】:

          【解决方案5】:

          一般:

          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);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-01-25
            • 1970-01-01
            • 1970-01-01
            • 2011-07-23
            • 2020-09-15
            • 2021-04-15
            • 2021-03-03
            • 1970-01-01
            相关资源
            最近更新 更多