2017-06-17 55 views
2

请帮助我我弄不清楚运营商(和,不,XOR,..等)如何在java中工作。我知道AND和OR的输出,但我在无所不知的情况下。例如,我不完全理解诸如变量!= integer(i!= 3)的语句。我的意思是NOT操作符是如何工作的。例如,在这里如何工作。运算符(如NOT(!),AND(&&)等)如何在java中工作?

class Demo { 

    public static void main(String args[]) throws java.io.IOException { 
     char ch; 

     do { 

      System.out.print("Press a key followed by ENTER: "); 

      ch = (char) System.in.read(); // get a char 

     } while (ch != 'q'); 

    } 

} 
+0

在你的情况循环,直到声道输入不等于'q'这是什么意思'=' –

+0

这就是“不等于”!运营商。 “不”运算符是'!'你知道一个数字与3有什么不同吗? – molbdnilo

+0

哦,好的。这是否意味着如果(i!= 3)意味着3不等于i。说到整数我不明白这个概念。 –

回答

0

,如果你做一些系统出局,你会弄明白:

char ch = 'l'; 
System.out.print(2 != 3); 
--true, they are not equal 
System.out.print('q' != 'q'); 
-- false, they are equals 
System.out.print(ch != 'q'); 
-- true, they are not equals 

这意味着,他们!=检查,如果他们是完全一样的(在这种情况下谨慎使用基本类型,例如如int,char,bool等。这个操作符在对象中的工作方式不同,如String)

+1

''q''是一个'char',而不是'String'。 – molbdnilo

+0

@molbdnilo你是完全正确的,我现在正在编辑 –

+1

@Aominè也是对的,我读得很快,没有注意到简单的引用'q',我认为它是“q”,非常感谢 –

0

The!操作符是一元的。当施加到其切换布尔值,例如

bool on = true; 
System.out.print(!on); //false 
System.out.print(on); //true 

时旁边的一个等号使用时,它检查该值是不等于。如果不相等,则返回true。否则,它返回false。例如,

int two = 2; 
int three = 3; 
System.out.print(two != three); 
//returns true, since they are not equal 
System.out.print(two == three); 
//returns false, since they are not equal 
+0

“'!'操作符是二进制的。”实际上,它是一元操作符http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html http://docs.oracle.com/javase/specs/jls/se8/html/jls-15 .html#jls-15.15.6 –

+1

@Lew Bloch,我的错误,更正了 –

+0

“当在等号旁边使用['!'is]时”有些人可能会误解这意味着'!='包含两个令牌,或者两个令牌运算符连接在一起,而不是。 '!='是一个单独的运算符,并被解析为单个Java令牌。 –

0
int x = 4; 
int y = 5; 

!case a装置不区分一个

x == y装置的x和y正在引用在存储器中的相同位置(以明白这意味着什么,见this question)。

x != y是上述的相反,并且是相同的写入!(x == y)(未x等于y)的

case a && case b And运算 - 两个情况下的情况b都为真。

case a || case b或运营商 - 无论是哪种情况a 情况b是真实的。

这里是相同的例子,让一切都更清晰:

1==1 // true, as 1 is equal to 1 
2==1 // false, as 1 is not equal to 2. 
!true // false, as not true is false. 
1!=1 // false, as 1 is equal to one. 
!(1==1) // The same as above - exact same output. 
// You need the brackets, because otherwise it will throw an error: 
!1==1 // Error - what's '!1'? Java is parsed left-to-right. 
true && false // false - as not all cases are true (the second one is false) 
true || false // rue - as at least one of the cases are true (the first one) 
相关问题