2017-03-14 42 views
0

所以這裏就是我想要的功能,看起來像:PlayFramework使一個函數返回一個整數在旋轉?

@getMaxLevel(buildingId : Integer) = @{ 
    if levelMap.get(buildingId) == null 1 else levelMap.get(buildingId) 
} 

但問題是,當我把這個我得到一個失蹤支架,其中有沒有(一個錯誤,因爲如果我更換內容有,說,3然後一切正常

我如何告訴捻有這個,因爲我在我的捻代碼中調用一個方法?

回答

0

你只是缺少if子句中的括號內的信息正確的。語法是:

@getMaxLevel(buildingId : Integer) = @{ 
    if (levelMap.get(buildingId) == null) 1 
    else levelMap.get(buildingId) 
} 

或者更好,使用containsKey,而不是領值的兩倍:

@getMaxLevel(buildingId : Integer) = @{ 
    if (!levelMap.containsKey(buildingId)) 1 
    else levelMap.get(buildingId) 
} 

如果您使用的是Java 8,也許你甚至不需要此功能,因爲你可以使用Map.getOrDefault

@getMaxLevel(buildingId : Integer) = @{ 
    levelMap.getOrDefault(buildingId, 1) 
} 
相關問題