定義一個長度為5的數組,存儲學生成績,學生的成績由鍵盤輸入:
1、將成績倒序打印輸出。
2、計算成績平均值。
3、給定一個合格線,輸出高於合格線的成績
import java.util.Scanner; public class HomeWork { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //成績操作 grade(sc); } //定義一個長度為5的數組,存儲學生成績,學生的成績由鍵盤輸入 public static void grade(Scanner sc){ //定義數組長度 int[] score = new int[5]; //輸入成績 gradesIn(sc,score); //成績倒序打印輸出 gradeReverse(score); //計算成績平均值 System.out.println("成績平均值為:"+mean1(score)); //給定一個合格線,輸出高於合格線的成績 gradeHigher(sc,score); } //成績輸入 public static int[] gradesIn(Scanner sc,int[] score){ System.out.println("請輸入成績:"); for(int i = 0;i < score.length;i++){ int num = sc.nextInt(); score[i] = num; } System.out.println("輸入的成績分別是:"); for(int i = 0;i < score.length;i++){ System.out.print(score[i] + " "); } System.out.println(); return score; } //成績倒序打印 public static void gradeReverse(int[] score){ System.out.println("倒序輸出的成績分別是:"); for(int i = score.length-1 ; i >= 0 ;i--){ System.out.print(score[i] + " "); } System.out.println(); } //計算數組平均值 public static double mean1(int[] arr1){ int sum = 0; for(int i = 0;i < arr1.length;i++){ sum += arr1[i]; } double meanNum = (double)sum/arr1.length; return meanNum; } //手動輸入一個成績作為成績的合格線,輸出高於合格線的成績 public static void gradeHigher(Scanner sc,int[] score){ System.out.println("請輸入你要選定的合格線:"); int passLine = sc.nextInt(); int j = 0; for(int i = 0; i < score.length ; i++){ if(score[i] > passLine){ System.out.println("高於合格線的成績有:"+score[i]); j++; } } if(j == 0){ System.out.println("沒有高於合格線的成績"); } } }
