Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-Boolean与If语句一起使用_Java_If Statement_Boolean - Fatal编程技术网

Java-Boolean与If语句一起使用

Java-Boolean与If语句一起使用,java,if-statement,boolean,Java,If Statement,Boolean,我刚在学校开始学习Java,我遇到了以下代码。我很难理解为什么输出显示:no。 既然x更新为100,布尔值不应该也更新为false,因此输出:yes 先谢谢你 这是我的密码: public static void main (String[] args) { Scanner keyboard = new Scanner(System.in); int x = -555; boolean isNegative = (x < 0);

我刚在学校开始学习Java,我遇到了以下代码。我很难理解为什么输出显示:
no
。 既然
x
更新为
100
布尔值不应该也更新为
false
,因此输出:
yes

先谢谢你

这是我的密码:

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

  }
publicstaticvoidmain(字符串[]args)
{
扫描仪键盘=新扫描仪(System.in);
int x=-555;
布尔值为负=(x<0);
如果(为负)
{
x=100;
如果(为负)
系统输出打印项次(“否”);
其他的
System.out.println(“是”);
} 
其他的
System.out.println(“可能”);
}

因此,在检查是否为负值之前,请将
x
设置为
-555
。然后将布尔值
isNegative
设置为x是否小于0,此时将为false。稍后在程序中更新x,以便if语句不受影响。它之所以说“不”,是因为它是否定的,在整个程序中不会更新。请记住,Java是自上而下运行的


我还注意到,打印“是”或“否”的代码位于负代码块中。你可能想解决这个问题

当整数
x
被更新时,布尔值
为负
未被更新。即使以后
x
被设置为100,
isNegative
已经被设置为
true
,并且在这一行之后
boolean isNegative=(x在函数精神中,您可以声明一个IntPredicate:

IntPredicate negative = (i)-> i < 0;
在编程中,谓词通常是一个方法/函数,它为某些输入返回一个布尔值。这里的输入是int,而对于基本类型,则有一个专门的谓词,如IntPredicate

虽然可以进行一些简短的构造,但总体开销相当高。请与自己进行比较:

示例代码:

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

为什么您认为布尔值不应该也更新?
x
已更新,但表达式
x<0
在更新
x
之前已计算过。更新变量不会自动更新使用该变量的表达式返回的值,如果这些表达式是在更新变量之前计算的。
 negative.test (-500)
 negative.test (500)
   boolean isNegative = (x < 0);
   if (isNegative)
   {
       x = 100;
       if (isNegative)
           System.out.println("no");
       else
           System.out.println("yes");
   } 
   else
       System.out.println("maybe");
   if (x < 0)
   {
       x = 100;
       if (x < 0)
           System.out.println("no");
       else
           System.out.println("yes");
   } 
   else
       System.out.println("maybe");
   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");
   IntStream is = ...
   is.filter (! negative).
      filter (prime).
      filter (square).
      filter (luckyNumber).
      filter (...