JS基础——数组综合练习(输入班级人数及成绩,求总成绩、平均成绩、最高分、最低分)

源码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
    var perCount = parseInt(prompt("请输入班级人数:"));
    var perScores = [];
    for (var i = 0; i < perCount; i++) {
        //成绩存入数组
        perScores[perScores.length] = parseInt(prompt("请输入第" + (i + 1) + "个学生的成绩:"));
    }
    var sum = 0;
    var max = perScores[0];
    var min = perScores[0];
    for (i = 0; i < perCount; i++) {
        //总成绩
        sum += perScores[i];
        //最高分
        if (max < perScores[i]) {
            max = perScores[i];
        }
        //最低分
        if (min > perScores[i]) {
            min = perScores[i];
        }
    }
    console.log(perScores);
    console.log("总成绩:" + sum);
    console.log("平均成绩:" + sum / perCount);
    console.log("最高分:" + max);
    console.log("最低分:" + min);
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/Xue_zenghui/article/details/82533547