我有这个groovy代码本身可以正常工作:
def msg = "$evt.displayName was locked7 ${userName ? "by " + userName + " " : ""}$lockMode" // Default message to send
当我把它放在这个简单的if else块中时,它根本不起作用(第一个条件和else条件都没有做任何事情)。
if (i == 999) { //For Schlage One touch lock function
def msg = "$evt.displayName was locked via One Touch" // Default message to send
} else {
def msg = "$evt.displayName was locked7 ${userName ? "by " + userName + " " : ""}$lockMode" // Default message to send
}
我的代码中是否有不正确的内容?
答案 0 :(得分:1)
如果在if或else之后有单行语句,则可以使用不带{}的if..else语句,对于多个语句,您必须使用花括号{}。
def msg = ""
int i = 999
if (i == 999)
msg = "$evt.displayName was locked via One Touch" // Default message to send
else
msg = "$evt.displayName was locked7 ${userName ? "by " + userName + " " : ""}$lockMode" // Default message to send
println "MESSAGE: "+msg
答案 1 :(得分:0)
有关您的问题的更多信息,
def
if
或else
内的变量无效(您的问题情景)def
无处不在(wkt)。def
,也可以使用下面显示的代码
的码强> 没有def msg
int i=999
if (i==999)
msg = "$evt.displayName was locked via One Touch" // Default message to send
else
msg = "$evt.displayName was locked7 ${userName ? "by " + userName + " " : ""}$lockMode" // Default message to send
println "MESSAGE: "+msg