코드 그라데이션

성적 출력 프로그램 본문

Java/간단한 Quiz

성적 출력 프로그램

완벽한 장면 2023. 1. 14. 12:34
public class Practice {
    public static void main(String[] args) {
        boolean run = true;
        int studentNum = 0;
        int[] scores = null;
        Scanner sc = new Scanner(System.in);
        while (run) {
            System.out.println("--------------------------------------------------------");
            System.out.println(" 1. 학생수 | 2. 점수입력 | 3. 점수리스트 |4. 분석 | 5. 종료");
            System.out.println("--------------------------------------------------------");
            System.out.println("선택> ");

            int selectNo = sc.nextInt();

            if (selectNo == 1) {
                // 작성위치
                System.out.println("학생 수: ");
                studentNum = sc.nextInt();
                // 학생 수 만큼의 배열을 만들어서 score변수에 저장해준다.
                scores = new int[studentNum]; //이렇게 하면 score 객체를 불러와서 index만큼 돌게 할 수도 있고, 활용할수도 있게 된다.,

            } else if (selectNo == 2) {
                // 작성위치
                // 반복적으로 학생의 점수 입력하면 되니까 반복문 사용
                for (int i = 0; i< scores.length; i++) {
                    System.out.println("scores[" + i + "] : "); //scores의 i번째 값을 받게 만든다.
                    int score = sc.nextInt();
                    // 들어온 score값은 각각 scores의 i번째에 담아주면 된다.
                }
            } else if (selectNo == 3) {
                // 작성위치
                // 이것은 index를 돌면서 점수를 불러오기만 하면 된다!
                for (int i = 0; i<scores.length; i++) {
                    System.out.println("scores[" + i + "] : " + scores[i]);
                }
            } else if (selectNo == 4) {
                // 작성위치
                // 분석하려면 최댓값 있어야 하고, 평균을 내야 하니까 합계, 그리고 평균도 있어야 한다.
                int max = 0;
                int sum = 0;
                double avg = 0;

                // 최댓값은 삼항연산자로 구현해본다!
                for (int i = 0; i<scores.length; i++) {
                    max = (max < scores[i] ? scores[i] : max);
                // 합계는 끝까지 돌면서 scores의 i번째 값들을 다 누적시킨다.
                    sum += scores[i];
                }
                // 평균은 이 연산이 다 끝난 다음에 합계를 총 학생수로 나눠주면 나온다.
                avg = (double) sum/studentNum; //double로 강제 형변환!
                System.out.println("최고 점수: " + max);
                System.out.println("평균 점수: " + avg);


            } else if (selectNo == 5) {
                run = false;
            }
        }
        System.out.println("프로그램 종료");
    }
}
728x90

'Java > 간단한 Quiz' 카테고리의 다른 글

스트림 퀴즈  (0) 2023.02.24
For문과 For-each문의 직관적 비교 예시  (0) 2023.01.19
숫자 UP-DOWN 게임  (0) 2023.01.14
가위바위보 게임 만들기  (0) 2023.01.14
숫자를 입력받아 거꾸로 출력하기  (0) 2023.01.14
Comments