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

发布于 2024-11-14 09:40:17 字数 521 浏览 6 评论 0原文

我是 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)
    }   
}

I'm new to Java and working on a basic program that looks through an array and gives prints the amount of numbers in the array that are divisible by 3. I'm having some trouble getting it to work right. Here is the code that I've got so far.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ偶尔ゞ 2024-11-21 09:40:17

试试这个(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);
}

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

Try this (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);
}

Java 8 update:

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);
}
终难遇 2024-11-21 09:40:17

您需要一个 for 循环来按顺序评估数组中的每个项目:

 int[] numarray = { 1, 2, 3 };
 for (int i = 0; i < numarray.Length; i++)
 {
     if (numarray[i] % 3 == 0)
     {
         b++;
     }
 }

You'll need a for loop to evaluate each item in the array sequentially:

 int[] numarray = { 1, 2, 3 };
 for (int i = 0; i < numarray.Length; i++)
 {
     if (numarray[i] % 3 == 0)
     {
         b++;
     }
 }
小镇女孩 2024-11-21 09:40:17

请尝试:

   int b=0;        

   int[] numarray ={3, 9, 45, 88, 23, 27, 68};

   for ( int i=0; i<numarray.length; i++)
   {   
       if (numarray[i]%3==0)
           b++;
   }   
   System.out.println("This is the amount of numbers divisible by 3:" +b)

Please try :

   int b=0;        

   int[] numarray ={3, 9, 45, 88, 23, 27, 68};

   for ( int i=0; i<numarray.length; i++)
   {   
       if (numarray[i]%3==0)
           b++;
   }   
   System.out.println("This is the amount of numbers divisible by 3:" +b)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文