1. 程式人生 > 其它 >專案 3 統計考試成績

專案 3 統計考試成績

技術標籤:# 專案java專案統計考試成績

專案 3 統計考試成績

  • 請編寫一個 Java 應用程式讀入多個考試成績值,統計顯示輸入的成績個數,最低成績
    和最高成績。
    分數值在[0,100]範圍內,-999 表示結束輸入。當輸入的分數值小於 0 或大於 100 時,給
    出提示資訊要求重新輸入。參與統計的成績均為有效成績,也就是說小於 0或大於 100 或-999
    是不在統計之列的。
    資料輸入輸出的格式不做限制,你可以使用Scanner類從控制檯輸入,然後控制檯輸出;
    也可以使用 JOptionPane 類按 GUI 的方式輸入輸出。只要選擇其中任意一種即可,不要提交
    多個版本的程式。
    另外進一步統計輸出不同成績等級(A,B,C,D,F)人數的同學可以得到最多 5 分的加分。A

    等級為[90,100],B 等級為[80,90),C 等級為[70,80),D 等級為[60,70),F 等級為 60 以下。

  • 參考程式碼:

    package ThirdProject;
    
    import java.util.Scanner;
    
    public class LuoYu_3 {
        public static void main(String[] args) {
            System.out.print("Enter grade from 0 - 100,-999 to stop:");
            Scanner input = new Scanner(System.in)
    ; int number; int min = (int)1e9; int max = -1; int count = 0,countOfA = 0,countOfB = 0,countOfC = 0,countOfD = 0,countOfF = 0; while ((number = input.nextInt()) != -999){ if (number > 100 || number < 0) { System.out.println("Error: That is not a valid score."
    ); System.out.print("Enter grade from 0 - 100,-999 to stop:"); continue; } count++; if (number > max) max = number; if (number < min) min = number; if (number >= 90) countOfA++; if (number >= 80 && number < 90) countOfB++; if (number >= 70 && number < 80) countOfC++; if (number >= 60 && number < 70) countOfD++; if (number < 60) countOfF++; System.out.print("Enter grade from 0 - 100,-999 to stop:"); } System.out.println(); System.out.println("Number of grades entered:" + count); System.out.println("Highest grade entered:" + max); System.out.println("Lowest grade entered:" + min); System.out.println(); System.out.println("As: " + countOfA); System.out.println("Bs: " + countOfB); System.out.println("Cs: " + countOfC); System.out.println("Ds: " + countOfD); System.out.println("Fs: " + countOfF); } }
  • 顯示結果:

    Enter grade from 0 - 100,-999 to stop:39
    Enter grade from 0 - 100,-999 to stop:80
    Enter grade from 0 - 100,-999 to stop:82
    Enter grade from 0 - 100,-999 to stop:88
    Enter grade from 0 - 100,-999 to stop:91
    Enter grade from 0 - 100,-999 to stop:99
    Enter grade from 0 - 100,-999 to stop:100
    Enter grade from 0 - 100,-999 to stop:0
    Enter grade from 0 - 100,-999 to stop:-2
    Error: That is not a valid score.
    Enter grade from 0 - 100,-999 to stop:101
    Error: That is not a valid score.
    Enter grade from 0 - 100,-999 to stop:62
    Enter grade from 0 - 100,-999 to stop:-999
    
    Number of grades entered:9
    Highest grade entered:100
    Lowest grade entered:0
    
    As: 3
    Bs: 3
    Cs: 0
    Ds: 1
    Fs: 2
    
    Process finished with exit code 0