Mat*_*att 3 java javafx return checkstyle
目前,我正在为Java类编写打砖块游戏类。我对我的public javafx.scene.paint.Color getColor()方法遇到的一个检查样式错误有疑问,该方法根据被球击中的次数获取砖的颜色。
checkstyle错误说:Return count is 6 (max allowed for non-void methods/lambdas is 2).我认为这意味着我只能有2条返回语句,而我目前只有6条。所有的语句都有不同的返回值,基于这些值this.hits我不了解。其文档如下:
public javafx.scene.paint.Color getColor()
The current color to represent this Brick's breakability state.
Returns:There are five possible Colors that can be returned based on 
theBrick's current strength: Color.BLACK if this Brick cannot be 
broken;Color.WHITE if this Brick has been completely broken; and 
Color.RED, Color.YELLOW,Color.GREEN if this Brick will break after 1, 2, 
and 3 more hits, consecutively.
和代码:
public javafx.scene.paint.Color getColor() {
        if (this.hits == -1) {
            return javafx.scene.paint.Color.BLACK;
        } else if (this.hits == 0) {
            return javafx.scene.paint.Color.TRANSPARENT;
        } else if (this.hits == 1) {
            return javafx.scene.paint.Color.RED;
        } else if (this.hits == 2) {
            return javafx.scene.paint.Color.YELLOW;
        } else if (this.hits == 3) {
            return javafx.scene.paint.Color.GREEN;
        }
        return null;
}
您可以HashMap<Integer, Color>使用自己的值填充,并使用一次回车操作从中获取值Map。喜欢,
private static Map<Integer, javafx.scene.paint.Color> COLOR_MAP = new HashMap<>();
static {
    COLOR_MAP.put(-1, javafx.scene.paint.Color.BLACK);
    COLOR_MAP.put(0, javafx.scene.paint.Color.TRANSPARENT);
    COLOR_MAP.put(1, javafx.scene.paint.Color.RED);
    COLOR_MAP.put(2, javafx.scene.paint.Color.YELLOW);
    COLOR_MAP.put(3, javafx.scene.paint.Color.GREEN);
}
public javafx.scene.paint.Color getColor() {
    return COLOR_MAP.get(this.hits);
}
以下方法对一个return语句执行相同的操作:
    public javafx.scene.paint.Color getColor() {
        javafx.scene.paint.Color color = null;
        if (this.hits == -1) {
            color = javafx.scene.paint.Color.BLACK;
        } else if (this.hits == 0) {
            color = javafx.scene.paint.Color.TRANSPARENT;
        } else if (this.hits == 1) {
            color = javafx.scene.paint.Color.RED;
        } else if (this.hits == 2) {
            color = javafx.scene.paint.Color.YELLOW;
        } else if (this.hits == 3) {
           color = javafx.scene.paint.Color.GREEN;
        }
        return color;
 }
| 归档时间: | 
 | 
| 查看次数: | 92 次 | 
| 最近记录: |