码迷,mamicode.com
首页 > 编程语言 > 详细

23.使用数组计算平均分

时间:2016-05-27 23:28:03      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.Scanner;

public class ArrayDemo {

    /**
     * 使用数组计算平均分
     */
    public static void main(String[] args) {
        int[] scores = new int[5];    //成绩数组
        int sum = 0;                //成绩总和
        Scanner input = new Scanner(System.in);
        System.out.println("请输入5位学员的成绩:");
        for(int i = 0; i < scores.length; i++){
            scores[i] = input.nextInt();
            sum = sum + scores[i];    //成绩累加
        }
        /*//使用foreach遍历
        for(int i = 0; i < scores.length; i++){
            scores[i] = input.nextInt();
        }
        for(int score:scores){
            sum+=score;
        }*/
        //计算并输出平均分
        System.out.println("学员的平均分是:" + (double)sum/scores.length);
    }
}

 

23.使用数组计算平均分

标签:

原文地址:http://www.cnblogs.com/xiaotaoxu/p/5536320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!