请帮助我,我无法弄清楚运算符(AND,NOT,XOR ,, .. ETC)如何在java中工作。我知道AND和OR的输出,但我对NOT无能为力。例如,我不完全理解变量!=整数(i!= 3)等语句。我的意思是NOT运算符是如何工作的。例如,如何在这里不起作用。
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 :(得分: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等。此运算符在对象中的工作方式不同,例如字符串)
答案 1 :(得分:0)
!运营商是一元的。当应用于布尔值时,它会切换,例如
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
答案 2 :(得分: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
和运算符 - 和案例b都属实。
case a || case b
或运算符 - 无论是还是情况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)