package com.zhen.array; //数组的使用 普通的for 循环 public class ArrayDemo03 { public static void main(String[] args) { int[] a = {1,2,3,4,5,6}; //打印数组的全部元素 for (int i = 0; i < a.length; i++) {//for 遍历数组的作用 System.out.println(a[i]); } //计算所有元素的和 int sum = 0; for (int i = 0; i <a.length ; i++) { sum= sum+a[i]; } System.out.println(sum); //查找最大元素 int max = a[0]; for (int i = 0; i <a.length ; i++) { if (max<a[i]){ max=a[i]; } } System.out.println(max); } }
1
2
3
4
5
6
21
6