!type 在此代码中是什么意思?

What does !type mean in this code?

以下代码段中的 !type 是什么意思?为什么要放!

String type = request.getParameter("tipo");
if (type == null) {
    out.print("ERROR: The field type wasn't selected<br>");
}
if (!type.equals("auto")
&&  !type.equals("trailer")
&&  !type.equals("motorcycle")) {
    out.print("ERROR: field error ("+type+")<br>");
}

有人可以向我特别解释一下这些代码吗!type

'!'是一个布尔运算符,它只是表示 NOT(否定)。

so !type.equals("auto") 如果类型不是 "auto"

,则计算结果为真

Type 是 String 对象的一个​​实例,它有方法 String#equals(...) 和那个方法 returns a boolean...

"!" 这是否定运算符并反转任何布尔值...

所以 !type.equals("auto") 是一个 boolean 条件,是比较名称类型的字符串变量是否具有值 "auto" 的结果。

!不是,并且 equals() 方法 return 布尔值,这意味着它 returns true 和 false 然后!将否定它,它使 true , false 和 false 为真例如:

String text = "test";

Text.equals("test") returns true
And !text.equals("test") returns false

Text.equals("example") returns false
And !text.equals("test") returns true

或者只是在您的代码中,这意味着检查文本是否不等于...