我如何压缩这些 return 语句或一起避免 checkstyle 错误?

How do i condense these return statements or avoid the checkstyle error all together?

目前,我正在为我的 java class 编写 classes 的破砖游戏。我对我的 public javafx.scene.paint.Color getColor() 方法遇到的 checkstyle 错误有疑问,该方法根据砖块被球击中的次数获取砖块的颜色。

checkstyle 错误说:Return count is 6 (max allowed for non-void methods/lambdas is 2). 我认为这意味着我只能有 2 个 return 语句,而不是我目前的 6 个。根据 this.hits 的值,所有语句都有不同的 returns,这是我不明白的。它的文档如下:

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>,然后使用单个 return 从 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;
 }

完成@cOder 和@Elliot 的回答,您可以使用switch-case 来改进您的代码。在您的情况下,使用此体系结构比 运行 更快,并且您不必将 HashMap 保存到内存中即可使其工作。

public javafx.scene.paint.Color getColor() {
    javafx.scene.paint.Color color;
    switch (this.hits) {
        case -1:
            color = javafx.scene.paint.Color.BLACK;
            break;
        case 0:
            color = javafx.scene.paint.Color.TRANSPARENT;
            break;
        case 1:
            color = javafx.scene.paint.Color.RED;
            break;
        case 2:
            color = javafx.scene.paint.Color.YELLOW;
            break;
        case 3:
            color = javafx.scene.paint.Color.GREEN;
            break;
        default:
            color = null;
            break;
    }

    return color;
}

您可以看一下 this request,因为它略微解释了我们的 3 个答案之间的区别。