计算所有元素的和
package com.guoba.array;
public class Demo03 {
public static void main(String[] args) {
//定义数组
int[] arr = {1,2,3,4,5};
//打印数组全部元素
for (int i=0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("==========================");
//计算所有元素的和
int sum = 0;
for (int i=0; i < arr.length; i++) {
sum = sum + arr[i];
}
System.out.println("sum"+sum);
System.out.println("==================");
//查找最大元素
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max){
max = arr[i];
}
}
System.out.println("max:"+ max);
}
}