본문 바로가기
Java

이것이 자바다 - 챕터5 연습 문제

by Jiwon_Loopy 2025. 4. 5.
반응형

8번


 		int[][] array = { { 95, 86 }, { 83, 92, 96 }, { 78, 83, 93, 87, 88 } };
		int sum = 0;
		int cnt = 0;
		
		for(int i = 0; i<array.length;i++) {
			for(int j =0; j<array[i].length; j++) {
				sum += array[i][j];
				cnt++;
			}
		}
		
		System.out.println("전체 합 :" + sum + "\\n" + "평균 :" + ((double)sum/cnt));

9번


   	Scanner sc = new Scanner(System.in);
		int students = 0;
		int[] scores = null;

		while (true) {
			System.out.println("-----------------------------------------------");
			System.out.println("1.학생수 | 2.점수입력 | 3.점수리스트 | 4.분석 | 5.종료");
			System.out.println("------------------------------------------------");
			System.out.println("선택> ");
			int select = Integer.parseInt(sc.nextLine());

			switch (select) {
			case 1:
				System.out.println("학생수> ");
				students = Integer.parseInt(sc.nextLine());
				scores = new int[students];
				break;
			case 2:
				for (int i = 0; i < students; i++) {
					System.out.println("scores[" + i + "]:> ");
					scores[i] = Integer.parseInt(sc.nextLine());
				}
				break;
			case 3:
				for (int i = 0; i < students; i++) {
					System.out.println("scores[" + i + "]:> " + scores[i]);
				}
				break;
			case 4:
				int[] result = calcScore(scores);
				System.out.println("최고 점수: " + result[0]);
				System.out.println("평균 점수: " + (double)result[1] / students);
				break;
			case 5:
				return;
			}
		}

	}
	
	static int[] calcScore(int[] scores) {
		int max = 0;
		int sum = 0;
		for(int score : scores) {
			max = Math.max(score, max);
			sum += score;
		}
		return new int[] {max, sum};
	}

package ch04;

import java.util.Scanner;

public class ch04 {

	public static void main(String[] args) {

		// 9번
		Scanner sc = new Scanner(System.in);
		int students = 0;
		int[] scores = null;
		int select = 0;
		while (true) {
			System.out.println("-----------------------------------------------");
			System.out.println("1.학생수 | 2.점수입력 | 3.점수리스트 | 4.분석 | 5.종료");
			System.out.println("------------------------------------------------");
			System.out.println("선택> ");
			
			select = inputValue(sc);
			
			switch (select) {
			case 1:
				System.out.println("학생수> ");
				students = inputValue(sc);
				scores = new int[students];
				break;
			case 2:
				for (int i = 0; i < students; i++) {
					extracted(i);
					scores[i] = inputValue(sc);
				}
				break;
			case 3:
				for (int i = 0; i < students; i++) {
					extracted(i);
					System.out.println(scores[i]);
				}
				break;
			case 4:
				int[] result = calcScore(scores);
				System.out.println("최고 점수: " + result[0]);
				System.out.println("평균 점수: " + (double) result[1] / students);
				break;
			case 5:
				return;
			}
		}

	}

	private static void extracted(int i) {
		System.out.println("scores[" + i + "]:> ");
	}
	
	static int inputValue(Scanner sc) {
		int select = 0;
		try {
			select = Integer.parseInt(sc.nextLine());
		} catch (NumberFormatException e) {
			System.out.println("숫자만 입력하실 수 있습니다.");
		}
		return select;
	}

	static int[] calcScore(int[] scores) {
		int max = 0;
		int sum = 0;
		for (int score : scores) {
			max = Math.max(score, max);
			sum += score;
		}

		return new int[] { max, sum };
	}
}

728x90
반응형