集册 Java实例教程 使用增强的for语句计算数组中的整数总数。

使用增强的for语句计算数组中的整数总数。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

484
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用增强的for语句计算数组中的整数总数。

public class Main 

{

   public static void main(String[] args) 

   {
   /*
   N  o w  J a v a . c o m 提供
   */

      int[] array = {87, 68, 94, 10, 83, 78, 851, 91, 76, 87};

      int total = 0;


      // add each element's value to total

      for (int number : array)

         total += number;


      System.out.printf("Total of array elements: %d%n", total);

   } 

}