for循环遍历数组(数组1)
1 package com.mon10.day24; 2 3 import java.util.Scanner; 4 5 /** 6 * 类说明 :计算学生的总成绩和平均分 7 * @author 作者 : chenyanlong 8 * @version 创建时间:2017年10月24日 9 */ 10 public class ArraysTest { 11 12 public static void main(String[] args) { 13 System.out.println("请输入5名同学的平均成绩"); 14 Scanner input=new Scanner(System.in); 15 int[] arrays=new int[5];//定义数组 16 int sum = 0; 17 double avg; 18 19 //循环输入 20 for(int i=0;i<5;i++){ 21 arrays[i]=input.nextInt(); 22 } 23 24 //for循环遍历数组 25 for(int k : arrays){ 26 System.out.println("k:"+k); 27 sum=sum+k; 28 } 29 30 //平均成绩 31 avg=sum/5; 32 33 //输出总成绩,平均成绩 34 System.out.println("总成绩为:"+sum); 35 System.out.println("平均成绩为:"+avg); 36 } 37 }
显示效果: