大家可以关注一下专栏,方便大家需要的时候直接查找,专栏将持续更新~
题目描述
编写一个程序,统计十个同学的成绩,计算并输出他们的最高分、最低分和平均分。
解题思路
- 创建一个整型数组来存储十个同学的成绩。
- 使用循环遍历数组,找到其中的最高分和最低分,同时累加总成绩。
- 根据累加的总成绩计算平均分,即总成绩除以同学数量。
源码答案
public class ScoreStatistics {
public static void main(String[] args) {
// 定义一个数组来存储十个同学的成绩
int[] scores = {90, 85, 88, 92, 95, 87, 84, 91, 89, 93};
// 计算最高分、最低分和总分
int maxScore = scores[0];
int minScore = scores[0];
int totalScore = 0;
// 遍历数组
for (int score : scores) {
// 更新最高分和最低分
if (score > maxScore) {
maxScore = score;
}
if (score < minScore) {