计算学生成绩
package Remain;
import java.util.Scanner;
public class Demo11 {
public static void main(String[] args) {
// 统计一组学生成绩
// 声明部分
double[] scores = new double[10]; // 双精度一维数组,10个元素,scores[0], scores[1],..., scores[9]
double sum, average, max, min;
Scanner sc = new Scanner(System.in);
// 输入部分
for (int i = 0; i < scores.length; i++) {
System.out.print("scores[" + i + "] = ");
scores[i] = sc.nextDouble();
}
// 处理部分
sum = 0;
for (double score : scores) {
sum = sum + score;
}
average = sum / scores.length;
max = Double.MIN_VALUE;
for (double score : scores) {
if (score > max) {
max = score;
}
}
// for (int i = 0; i < scores.length; i++) {
// if (scores[i] < min) {
// min = scores[i];
// }
// }
// 增强for的优缺点
//只能从头到尾的遍历数组或集合,而不能只遍历部分;
//在遍历List或数组时,不能获取当前元素下标;
//增强for使用便简单,这是它唯一的优点了;
//增强for比使用迭代器方便一点!
min = Double.MAX_VALUE;
for (double score : scores) {
if (score < min) {
min = score;
}
}
// 输出部分
System.out.println("sum = " + sum);
System.out.println("average = " + average);
System.out.println("max = " + max);
System.out.println("min = " + min);
}
}