简体   繁体   English

Java - 与 If 语句一起使用的布尔值

[英]Java - Boolean used with If statement

I just started learning Java at school and I encountered the following piece of code.我刚开始在学校学习Java,遇到了以下代码。 I have trouble understanding why the output shows: no .我无法理解为什么输出显示: no Since x is updated to 100 shouldn't the boolean also update to false and therefore output: yes ?由于x更新为100不应该boolean也更新为false并因此输出: yes吗?

Thank you in advance先感谢您

Here's my code:这是我的代码:

   public static void main (String[] args)
   {
       Scanner keyboard = new Scanner(System.in);
       int x = -555;
       boolean isNegative = (x < 0);
       if (isNegative)
       {
           x = 100;
           if (isNegative)
             System.out.println("no");
          else
             System.out.println("yes");
       } 
       else
          System.out.println("maybe");

  }

So, you set x to -555 before you check if it's negative.因此,在检查它是否为-555之前,将x设置为-555 you then set the boolean isNegative to whether x is smaller than 0, at which point it will be false.然后将布尔值isNegative设置为 x 是否小于 0,此时它将为 false。 You update x later in the program, so that if statement is not affected.稍后在程序中更新 x,以便 if 语句不受影响。 The reason it's saying no is because it's given is negative, which is not updated throughout the program.之所以说不,是因为它给出的是否定的,在整个程序中都没有更新。 Keep in mind Java runs from top to bottom.请记住,Java 从上到下运行。

Also something I noticed, is that the code that prints out yes or no is in the block of code that runs if it's negative.我还注意到,打印出是或否的代码在运行的代码块中,如果它是负数。 You may want to fix that.你可能想解决这个问题。

While the integer x was updated, the boolean isNegative was not.虽然更新了整数x ,但布尔值isNegative没有更新。 Even though x is set to 100 later on, isNegative has already been set to true , and after that one line boolean isNegative = (x<0);即使稍后将x设置为 100, isNegative也已经设置为true ,并且在那一行之后boolean isNegative = (x<0); , isNegative no longer depends on the value of x . , isNegative不再依赖于x的值。

In the code below, the output is "yes" , because isNegative is updated after x is updated.在下面的代码中,输出为"yes" ,因为isNegativex更新后更新。

   int x = -555;
   boolean isNegative = (x < 0);
   if (isNegative)
   {
       x = 100;
       isNegative = (x<0);
       if (isNegative)
         System.out.println("no");
      else
         System.out.println("yes");
   } 
   else
      System.out.println("maybe");

And ignore my formatting mistakes on my part as well, this is my first time answering ;)并忽略我的格式错误,这是我第一次回答;)

In functional spirit, you could declare an IntPredicate:本着功能精神,您可以声明一个 IntPredicate:

IntPredicate negative = (i)-> i < 0;

Note the martial arrow operator!注意武术箭头操作符! :) :)

Usage:用法:

 negative.test (-500)
 negative.test (500)

A predicate, in programming, is usually a method/function, which returns a boolean for some input.在编程中,谓词通常是一个方法/函数,它为某些输入返回一个布尔值。 Input is here an int, and for the basic types, there a specialized Predicates like IntPredicate.输入在这里是一个整数,对于基本类型,有一个像 IntPredicate 这样的专用谓词。

While a somewhat brief construction is possible, the overall overhead is pretty high.虽然可以进行一些简短的构建,但总体开销相当高。 Compare yourself:比较一下自己:

Sample code:示例代码:

   boolean isNegative = (x < 0);
   if (isNegative)
   {
       x = 100;
       if (isNegative)
           System.out.println("no");
       else
           System.out.println("yes");
   } 
   else
       System.out.println("maybe");

Lean code:精益代码:

   if (x < 0)
   {
       x = 100;
       if (x < 0)
           System.out.println("no");
       else
           System.out.println("yes");
   } 
   else
       System.out.println("maybe");

Predicate usage:谓词用法:

   IntPredicate negative = (i)-> i < 0;
   if (negative.test (x))
   {
       x = 100;
       if (negative.test (x))
           System.out.println("no");
       else
           System.out.println("yes");
   } 
   else
       System.out.println("maybe");

It has it's usage surely for more complicated tests, which you only want to write once, and which aren't that self documenting like (x < 0), and where a bunch of such tests can be combined:它确实适用于更复杂的测试,您只想编写一次,并且不是像 (x < 0) 那样的自我记录,并且可以组合一堆这样的测试:

   IntStream is = ...
   is.filter (! negative).
      filter (prime).
      filter (square).
      filter (luckyNumber).
      filter (...

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

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