Computer Science/Object Oriented Programming

[JAVA] 이것이 자바다(3판) 5장 9번 답안

아란정 2025. 4. 15. 17:42
728x90
반응형

9. 키보드로 학생 수와 각 학생들의 점수를 입력받고 최고 점수 및 평균 점수를 출력하는 코드를 작성해보세요.

package ch05.referenceType;

import java.util.Scanner;

public class Chap5 {
    public static void main(String[] args) {

        boolean run = true;
        Scanner sc = new Scanner(System.in);

        int studentNum = 0;
        Integer[] scoreList = null;
        int max = 0;
        int total = 0;

        while (run) {
            System.out.println(""" 
                    
                    -----------------------------------------------------------------------------------------
                    1. The number of Student || 2. Insert Score || 3. Score List || 4. Analysis || 5. Close
                    -----------------------------------------------------------------------------------------
                    """);
            System.out.println("Enter the number in a given order: ");
            int n = sc.nextInt();

            switch (n) {
                case 1: {
                    System.out.println("Enter the student number: ");
                    studentNum = sc.nextInt();
                    scoreList = new Integer[studentNum];
                    continue;
                }
                case 2: {
                    for (int i = 0; i < studentNum; i++) {
                        System.out.println("Enter student " + (i + 1) + " score: ");
                        scoreList[i] = sc.nextInt();
                    }
                    continue;
                }
                case 3: {
                    if (scoreList == null) {
                        System.out.println("return to prev progress");
                        continue;
                    }
                    for (int i = 0; i < studentNum; i++) {
                        System.out.println("student " + (i + 1) + " score: " + scoreList[i]);
                    }
                    continue;
                }
                case 4: {
                    if (scoreList == null) {
                        System.out.println("return to prev progress");
                        continue;
                    }
                    for (int i = 0; i < studentNum; i++) {
                        if (scoreList[i] > max) {
                            max = scoreList[i];
                        }
                        total += scoreList[i];
                    }
                    System.out.println("Average score: " + total / studentNum);
                    System.out.println("Max score: " + max);
                    continue;
                }
                case 5: {
                    System.out.println("Close the program");
                    run = false;
                }
            }
        }
    }
}
728x90