打印数组中的数字可被3整除

-3 java arrays divide

我是Java的新手,正在开发一个基本程序,它可以查看数组并给出打印数组中可被3整除的数字.我在使其正常工作时遇到了一些麻烦.这是我到目前为止的代码.

package arraysearch;

public class Intsearch {

    public static void main(String[] args) {

    }

    public static void multiple_3 (int[] a, int b)  {
        b=0;        
    }
    {
        int[] numarray ={3, 9, 45, 88, 23, 27, 68};
        {
            if (numarray % 3)==0;
                b = b+1;
        }
        System.out.println("This is the amount of numbers divisible by 3:" +b)
    }   
}
Run Code Online (Sandbox Code Playgroud)

Boh*_*ian 5

试试这个(Java 7):

public static void main(String[] args) {
    multiple_3(new int[] { 3, 9, 45, 88, 23, 27, 68 });
}

public static void multiple_3(int[] ints) {
    int count = 0;
    for (int n : ints) {
        if (n % 3 == 0) {
            count++;
        }
    }
    System.out.println("This is the amount of numbers divisible by 3: " + count);
}
Run Code Online (Sandbox Code Playgroud)

Java 8更新:

public static void multiple_3(int[] ints) {
    long count = IntStream.of(ints).filter(n -> n % 3 == 0).count();
    System.out.println("This is the amount of numbers divisible by 3: " + count);
}
Run Code Online (Sandbox Code Playgroud)