我是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)
试试这个(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)
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)
| 归档时间: |
|
| 查看次数: |
7907 次 |
| 最近记录: |