关于Java计算表达式

huangapple go评论61阅读模式
英文:

about java calculation expressions

问题

import java.util.*;

class Example{
public static void main(String asrg[]){
boolean b = false;
System.out.println(10>4 && true !=b==(10+3/2==8)==true); //Line 1
}

这是一个Java代码示例,主要是一个包含条件表达式的输出语句。请注意,我只会翻译代码部分,不会回答问题。

这个表达式的目的是计算一个布尔值,并将结果打印到控制台。具体计算的步骤如下:

  1. 首先,计算括号内的表达式:10 + 3 / 2 == 8。这里使用了算术运算和比较运算符,它们按照标准运算符优先级进行计算。结果为 false,因为 10 + 3 / 2 的值是 11,而 11 不等于 8。

  2. 接下来,比较 true != b。这里比较变量 true 是否不等于变量 b,其中 b 的值为 false。因此,这部分表达式为 true != false,结果为 true。

  3. 最后,比较 true == true,这是一个简单的布尔相等性比较,结果为 true。

  4. 将这些部分合并在一起,即 true && true。这是逻辑与运算,只有当两个操作数都为 true 时,结果才为 true。

  5. 最后,将结果打印到控制台,即 true。

英文:
import java.util.\*;

class Example{    
  public static void main(String asrg\[\]){    
    boolean b = false;    
    System.out.println(10\>4 && true !=b==(10+3/2==8)==true); //Line 1    
}

can anyone explain this expression?

expecting the steps how to calculate these

答案1

得分: 1

以下是已经翻译好的部分:

"The code provided is missing a final curly brace ('}') and will not compile. There are also some backslashes ('\') strung about that will result in syntax errors. Additionally, 'args' is misspelled but it is not necessary in order for code to compile.

With the final brace added and backslashes removed, the code is:

import java.util.*;

class Example
{    
  public static void main(String args[])
    {    
    boolean b = false;    
    System.out.println(10>4 && true != b == (10+3/2==8) == true); //Line 1    
    }
}

Running the above code will display the answer in console. However the steps can be explained below.

First, we need to simplify our boolean expression into just terms of 'true' and 'false'. Since we declare that 'b = false', and the fact that '10 > 4 == true' and '(10+3/2==8) == false', the expression that is printed can be simplified to '(true && true != false == false == true)'.

The expression can then be evaluated according to Java's operator precedence, and will evaluate to 'false'."

英文:

The code provided is missing a final curly brace ('}') and will not compile. There are also some backslashes ('\') strung about that will result in syntax errors. Additionally, 'args' is misspelled but it is not necessary in order for code to compile.

With the final brace added and backslashes removed, the code is:

import java.util.*;

class Example
{    
  public static void main(String args[])
    {    
    boolean b = false;    
    System.out.println(10>4 && true !=b==(10+3/2==8)==true); //Line 1    
    }
}

Running the above code will display the answer in console. However the steps can be explained below.

First, we need to simplify our boolean expression into just terms of true and false. Since we declare that b = false, and the fact that10 > 4 == true and (10+3/2==8) == false, the expression that is printed can be simplified to (true && true != false == false == true).

The expression can then be evaluated according to Java's operator precedence, and will evaluate to false.

答案2

得分: 0

Your code won't compile; It's missing a } at the end of the class.

Also, did you copy and past this from a website? Why are there \ all over the place?

For better readability, use the Java-style array declaration: String[] args instead of String args[].

To check a boolean value, you don't have to compare it to true or false. This includes any actual booleans, or expressions outputting a boolean, such as: 10 + 3 / 2 == 8, which is false.

英文:

Your code won't compile; It's missing a } at the end of the class.

Also, did you copy and past this from a website? Why are there \ all over the place?

For better readability, use the Java-style array declaration: String[] args instead of String args[].

To check a boolean value, you don't have to compare it to true or false. This includes any actual booleans, or expressions outputting a boolean, such as: 10 + 3 / 2 == 8, which is false.

huangapple
  • 本文由 发表于 2023年2月6日 15:38:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定