类型 GUI 中的方法 countTrue(boolean[]) 不适用于参数 (boolean, boolean, boolean, boolean, boolean)

The method countTrue(boolean[]) in the type GUI is not applicable for the arguments (boolean, boolean, boolean, boolean, boolean)

这是错误:

“类型 GUI 中的方法 countTrue(boolean[]) 不适用于参数(布尔值、布尔值、布尔值、布尔值、布尔值)” ,错误在最后一行,但我不明白为什么。

public class GUI {
    
    public GUI(){
        
    }
    
    public static int countTrue(boolean[] arr) {
        int count = 0;
        for (boolean element : arr) {
            if (element == true ) {
                count++;
            }
        }
        
        return count ;
    }
    
    
    public static void main(String[] args) {
        
        int x = GUI.countTrue([true, false, false, true, false]);
            
    }

}

不要在 countTrue 方法中使用 boolean [],而是将参数声明为 boolean...

新数组应如下声明:

int x = GUI.countTrue(new boolean[]{true, false, false, true, false});

您可以使用可变参数并将您的方法声明为: public static int countTrue(boolean... arr)

然后将其命名为: int x = GUI.countTrue(true, false, false, true, false);

您也可以将 if (element == true) 简化为 if (element)