简体   繁体   中英

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

this is the error:

"The method countTrue(boolean[]) in the type GUI is not applicable for the arguments (boolean, boolean, boolean, boolean, boolean)", the error is in the last line, but i dont get why.

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]);
            
    }

}

New array should be declared as below:

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

OR

you can use varargs and declare your method as: public static int countTrue(boolean... arr)

and then call it as: int x = GUI.countTrue(true, false, false, true, false);

Also you can simplify if (element == true) to just if (element)

Instead of boolean [] in your countTrue method, declare the parameter as boolean...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM