【问题标题】:Boolean use in an if statement在 if 语句中使用布尔值
【发布时间】:2018-04-17 07:24:22
【问题描述】:
public static int search(int[] a, int target)
{
    int i=0;
    boolean found = false;
    while((i<a.length) &&  ! found)
    {
        if (a[i] == target)
        {
        found = true;   
        }
        else i++;
    }
    if (found) return i;
    else return -1;
}

我不明白 if 语句部分。所以我在脑海中阅读它的方式被设置为假。如果没有找到......所以如果不是假的(因为找到=假),做任何事情。所以基本上我把它读作双重否定,看看(真)是否下降。但这没有意义。我知道它不是一个无限循环,它运行良好,但我不明白逻辑,它不能是双重否定。谢谢!

编辑: 所以我知道我们可以返回我,更容易是的,我同意。我只是在循环中使用非“!”的布尔值的逻辑遇到问题。符号。

基本上如果我写这个我会说(忽略其他一切)

found = true //found 开头为真 while (!found) //虽然不是真的 继续到下一个索引//继续直到...。实际上我现在变得非常困惑,因为要打破循环,我们将继续直到发现是假的,这在逻辑上是向后的

编辑:谢谢大家的cmets!这一切都帮助我理解了它!

【问题讨论】:

  • 这可以通过返回索引而不是found = true;来简单得多。然后在最后总是返回-1,因为只有在没有找到任何东西的情况下你才能到达它。 --- 不怪你没看懂,写的有点怪。
  • 拿一支笔和一张纸。然后手动运行它。
  • @GhostCat 经常说的话更容易理解。基本上发生的情况是您将found 设置为false,因此只要!found 就在while 循环内部 执行代码块——实际上是双重否定(found 是@987654327 @,所以 not falsetrue)。只要括号内的语句是truewhile only 就会执行。
  • 噢噢!!!我不知道while循环内的语句一定是真的。我认为这取决于你想要什么。所以基本上是“!”的意思。是使循环的这一部分为真,以便循环可以运行。然后当它找到时,我们将 found 设置为 true,现在是“!”使发现为假,我们退出。所以基本上循环内的部分我不应该直接翻译成英文。只是担心它是真还是假以保持循环运行?

标签: java if-statement boolean


【解决方案1】:

您对使用 IntStream 有何看法?

public static int search(int[] a, int target) {
    final OptionalInt index = IntStream.range(0, a.length).filter(i -> a[i] == target).findFirst();
    return index.isPresent() ? index.getAsInt() : -1;
}

【讨论】:

    【解决方案2】:

    设置boolean found = false;是因为你还没有找到你要找的东西,那么while(i&lt;a.length)! found意味着如果你还没有到达数组的末尾并且你还没有找到什么你正在寻找做

    {
        if (a[i] == target)
        {
        found = true;   
        }
        else i++;
    }
    

    如果你发现它把变量 found 更改为 true,使用否定运算符 ! 将更改为 false,&amp;&amp; 会将你带出 while 循环,因为这两个条件之一不为真不再。 换句话说,如果found 为假,那么在循环中为真,因为您有!(未)找到它。如果found 为真,那么在循环中为假,因为你已经找到它了。

    【讨论】:

    • 非常感谢!我没有意识到两件事都必须是真实的才能继续循环,我想如果你想要一件真实的事情和一件虚假的事情就做什么。我想两者都必须是真的,然后!有助于保持循环为真,直到发现为真!使其为假并退出。谢谢!!
    【解决方案3】:

    所以,我认为您的疑问在于 while 语句,但我会在所有代码上放置一些 cmets 以尝试解释它的作用:

        public static int search(int[] a, int target)
        {
            int i=0;
            boolean found = false;
    
        // While i index is less than the array length AND item hasn't been found (negation of the found variable), keep iterating on the loop. If not (if any of this two conditions aren't met), continue.
            while((i<a.length) &&  ! found)
            {
    /* If the targeted item has been found, set found boolean to true, so this loop will stop before next iteration
    */
                if (a[i] == target)
                {
    
                found = true;   
                }
    // If targeted item is not this one, increase the index for next iteration
                else i++;
            }
    /* This condition only will be met if we exit the loop because we found the target. In that case, we will return the index in the array of that index. If we "finished" the array without finding it, we will return -1, meaning "it wasn't found".
    */
            if (found) return i;
            else return -1;
        }
    

    【讨论】:

      【解决方案4】:
      public static int search(int[] a, int target)
      {
         for (int i = 0; i < a.length; i++){
              if (a[i] == target) return a[i]; // or i if you want to get ingex of searched element
         }
         return -1;
      }
      

      【讨论】:

      • 如果没有找到记得在最后返回-1
      • 请不要只提供代码答案。更喜欢使用解释性文本而不是嵌入的 cmets。这些很容易被忽视。
      【解决方案5】:

      在循环开始时发现是假的,所以while循环会开始。一旦找到一个值,found 设置为 true 并且循环停止。 但是我会重写循环:

      public static int search( int[] a, int target)
      {
          int foundIdx = -1;
          for( int i = 0; i < a.length && foundIdx < 0; i++ )
          {
               if( a[i] == target )
               {
                   foundIdx = i;
                   break;
               }
          }
          return foundIdx;
      }
      

      【讨论】:

        【解决方案6】:

        让我为你分解成几个部分:

        while((i<a.length) &&  ! found)
        

        所以你从 false 开始。当!false 评估为真时,您将继续循环,直到foundtrue,这将使整个条件为假,从而导致您中断循环。

        i&lt;a.length:当i&lt;length返回true时,继续循环。 您基本上希望 found 为 false 并且 i&lt;length 以使循环继续。如果不满足任何条件,则中断循环(查看&amp;&amp;)。

            if (a[i] == target) found = true;
            else i++;
        

        这很简单:如果找到数字,则将找到的布尔值设为真。这将导致下一个迭代条件评估为假,从而导致循环中断。

        【讨论】:

          【解决方案7】:

          你认为这更容易吗?

              public static int search(int[] a, int target) {
              for (int i = 0; i < a.length; i++) {
                  if (a[i] == target) {
                      return i;
                  }
              }
              return -1;
          }
          

          【讨论】:

          • 还有一些题外话:如果你不是必须使用while,你将使用更简单的循环,比如for。代码越少越好
          • 这能解释他的要求吗?
          • 问题是由错误的代码引起的。当我们简化代码时,我们就避免了这个问题。
          【解决方案8】:

          !found 与英语中的意思相同,即“未找到”。 i&lt;a.length 的整个条件为“而i 是一个有效的索引并且[target is] not found”,这非常接近,因为您知道“not found”是指target

          您可以简化此循环以避免布尔变量:

          while(i < a.length) {
              if (a[i] == target) {
                  return i;
              }
              i++;
          }
          return -1;
          

          【讨论】:

            【解决方案9】:

            代码在ints 数组上循环,而foundfalse。 在循环过程中,它将 (a[i]) 中的当前数组值与目标值进行比较,如果值相同,则将 found 设置为 true

            因此在循环之后它会检查found 是否为true,如果是则返回i,即最后一个比较值的索引。

            【讨论】:

              【解决方案10】:

              !运算符反转以下逻辑语句(逻辑非门)。

              因为 !false 为真,这意味着它不是双重否定。

              【讨论】:

                猜你喜欢
                • 2020-07-30
                • 2013-05-06
                • 2013-11-22
                • 1970-01-01
                • 2013-03-01
                • 2012-02-09
                • 2021-07-31
                • 2016-11-20
                相关资源
                最近更新 更多