Adjust Scores


Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type
Allowed languages
Java 19, Java 8

這個程式允許使用者輸入一串整數,第一個數字n表示後面接著n個數字,每個數字是一個學生成績。全部輸入完成之後,依照以下規則調整每個分數:

  1. 如果分數超過 55 且小於 60,則將其改為 60
  2. 如果分數超過 100,則將其改為 100 調整完之後,程式將修改後的成績清單完整印出。

這個程式目前仍缺少adjustScores函數的實作,請完成它,並且上傳完整的程式。 除了指定的adjustScores函數之外,請勿修改其他已經寫好的部分,違反此規則者,無論自動評測結果為何,在考試中均不計分。但允許輕微的排版差異。

程式完成之後,可以用以下測試資料做初步的測試,但仍請自行設計完整的測試資料

輸入 #1:

7 -1 0 59 60 61 100 101

輸出 #1:

-1 0 60 60 61 100 100

輸入 #2:

5 50 55 59 60 65

輸出 #2:

50 55 60 60 65

import java.util.Scanner;

public class AdjustScores {
    public static void main(String[] args) {
        // 讀取成績,並將結果存入 scores 陣列
        Scanner scanner = new Scanner(System.in);

        // 讀取成績的數量,這是使用者輸入的第一個數字,並創建一個長度為 n 的整數陣列
        int n = scanner.nextInt();
        int[] scores = new int[n];

        // 接著讀取 n 個成績,並將其存入 scores 陣列
        for (int i = 0; i < n; i++)
            scores[i] = scanner.nextInt();

        // 呼叫 adjustScores() 方法調整成績
        adjustScores(scores);

        // 印出調整後的每個成績
        for (int i = 0; i < n; i++)
            System.out.print(scores[i] + " ");

        scanner.close();
    }

    public static ... adjustScores(...) {
        // 這個方法的目的是調整陣列中的每個成績,使其符合規則
        // 如果成績大於 55 且小於 60,則改為 60
        // 如果成績大於 100,則改為 100
        ...
    }
}

Comments


  • 0
    scu09156146  commented on April 26, 2024, 3:42 p.m.

    題解

    import java.util.Scanner;
    
    public class AdjustScores {
        public static void main(String[] args) {
            // 讀取成績,並將結果存入 scores 陣列
            Scanner scanner = new Scanner(System.in);
    
            // 讀取成績的數量,這是使用者輸入的第一個數字,並創建一個長度為 n 的整數陣列
            int n = scanner.nextInt();
            int[] scores = new int[n];
    
            // 接著讀取 n 個成績,並將其存入 scores 陣列
            for (int i = 0; i < n; i++)
                scores[i] = scanner.nextInt();
    
            // 呼叫 adjustScores() 方法調整成績
            adjustScores(scores);
    
            // 印出調整後的每個成績
            for (int i = 0; i < n; i++)
                System.out.print(scores[i] + " ");
    
            scanner.close();
        }
    
        public static void adjustScores(int[] scores) {
            for (int i = 0; i < scores.length; i++) {
                if (scores[i] > 55 && scores[i] < 60) {
                    scores[i] = 60; // 成績大於 55 且小於 60,則改為 60
                } else if (scores[i] > 100) {
                    scores[i] = 100; // 成績大於 100,則改為 100
                }
            }
    
        }
    }