【问题标题】:What does !type mean in this code?!type 在这段代码中是什么意思?
【发布时间】:2016-03-19 10:54:17
【问题描述】:

!type 在下面的 sn-p 中是什么意思?为什么放!

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

【问题讨论】:

标签: java operators


【解决方案1】:

Type 是String 对象的一个​​实例,它具有String#equals(...) 方法并且该方法返回boolean...

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

所以 !type.equals("auto")boolean 条件,是比较名称类型的 String var 是否具有值 "auto" 的结果。

【讨论】:

    【解决方案2】:

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

    如果 type 不是“auto”,那么 !type.equals("auto") 将评估为 true

    【讨论】:

      【解决方案3】:

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

      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
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多