我如何表达两个值彼此不相等?
是否存在类似于equals()
的方法表示“不等于”?
我想要完成的一个例子如下:
if (secondaryPassword.equals(initialPassword)) { JOptionPane.showMessageDialog(null, "You've successfully completed the program."); } else { secondaryPassword = JOptionPane.showInputDialog(null, "Your passwords do not match. Please enter you password again."); }
我试图找到一些不需要我使用的东西if ( a != c)
。
只要放一个’!’ 在布尔表达式前面
“不等于”可以用“not”运算符表示!
和标准.equals
。
if (a.equals(b)) // a equals b if (!a.equals(b)) // a not equal to b
if (!secondaryPassword.equals(initialPassword))
如果该类实现可比性,您也可以这样做
int compRes = a.compareTo(b); if(compRes < 0 || compRes > 0) System.out.println("not equal"); else System.out.println("equal);
不使用!
,虽然不是特别有用,或可读….