作业1:
请输入10位同学的java成绩,
1、求平均成绩,最高成绩、最低成绩
2、对10的成绩按照从低到高的顺序排列(选做)
import java.util.Scanner;
/*
请输入10位同学的java成绩,
1、求平均成绩,最高成绩、最低成绩
2、对10的成绩按照从低到高的顺序排列(选做)
*/
public class Demo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入10位同学的java成绩:");
double[] grades = new double[10];
for (int i = 0; i < grades.length; i++){
grades[i] = sc.nextDouble();
}
for (double i : grades){
System.out.print(i + " ");
}
System.out.println();
System.out.println("平均成绩为:" + averageGrade(grades));
maxAndMin(grades);
System.out.println("成绩按照从低到高的顺序排列:");
sort(grades);
for (double i : grades){
System.out.print(i + " ");
}
}
public static double averageGrade(double[] arr){
double sum = 0;
for (int i = 0; i < arr.length; i++){
sum += arr[i];
}
double average = sum / arr.length;
return average;
}
public static void maxAndMin(double[] arr){
double max = arr[0], min = arr[0];
for (int i = 0; i < arr.length; i++){
if (max < arr[i]){
max = arr[i];
}
if (min > arr[i]){
min = arr[i];
}
}
System.out.println("最高成绩为:" + max + ",最低成绩为:" + min);
}
public static void sort(double[] arr){
for (int i = 0; i < arr.length - 1; i++){
for (int j = i + 1; j > 0; j--){
if (arr[j] < arr[j - 1])