728x90

 

배열 안에 배열값을 넣어서 배열 하나를 지정하고 그 안에 다시 배열값을 꺼내는 방식으로

[][] 이어서 꺼내온다

이중 for문을 이용하여 각 과목의 합계를 누적 합산하여 총점을구하고

해당 총점을 통해 length로 총점을 나눌 수를 구하여 avg를 계산

 

// 학생별 평균,총점
        int[][] score01 = {
                         {100,100,100}
                        ,{20,20,20}
                        ,{30,30,30}
                        ,{40,40,40}
                        ,{50,50,50}
                        };

        // 과목별 총점
        int korTotal = 0, engTotal = 0, mathTotal = 0;
        System.out.println("번호 국어 영어 수학 총점 평균");
        System.out.println("=============================");

        for(int i=0; i < score01.length;i++){
            int sum = 0;        // 각 개인 총점
            float avg = 0.0f;   // 각 개인 평균

            korTotal += score01[i][0];
            engTotal += score01[i][1];
            mathTotal += score01[i][2];
            System.out.printf("%3d", i+1); // 3d는 왼쪽으로 세칸 공간차지 // 번호 1~5 출력

            for(int jj=0;jj < score01[i].length;jj++) {
                sum += score01[i][jj];
                System.out.printf("%5d", score01[i][jj]);
            }

            avg = sum/(float)score01[i].length;  // 평균계산
            System.out.printf("%5d %5.1f%n", sum, avg);

        System.out.println("=============================");
        System.out.printf("총점:%3d %4d %4d%n", korTotal, engTotal, mathTotal);
/*  
    출력 결과

            번호 국어 영어 수학 총점 평균
            =============================================
            1  100  100  100  300 100.0
            =============================
            총점:100  100  100
            2   20   20   20   60  20.0
            =============================
            총점:120  120  120
            3   30   30   30   90  30.0
            =============================
            총점:150  150  150
            4   40   40   40  120  40.0
            =============================
            총점:190  190  190
            5   50   50   50  150  50.0
            =============================
            총점:240  240  240

*/
            
        }
728x90
반응형
728x90

 

평균 계산시, int / int = int가 나오므로 (float or double) 형변환 해줄 것

average = sum / (float)score.length; // 계산 결과를 float타입으로 형변환

 

총점과 평균

package practice03_jungsuk;

public class Ch5_89_array활용 {
	public static void main(String[] args) {
		
		int sum = 0;
		float average = 0f;
		
		int[] score = {100, 88 , 100, 100, 90};
		
		for(int i = 0; i < score.length; i++) {
			sum += score[i];
		}
		average = sum / (float)score.length; // 평균 연산시, 실수 형변환 잊지말것
		// casting안하면 정수로 결과도출 및 데이터 손실. (float)변환시 95.6 int시 95
		
		System.out.println("총점 : " +sum);
		System.out.println("평균 : " +average);
	}	
}

 

 

최대값 최소값

int[] score2 = {79,88,91,33,100,55,95}; 
		
int max = score2[0]; // 배열의 첫번째 값으로 최대값 초기화
int min = score2[0]; // 배열의 첫번째 값으로 최소값 초기화

for(int i=1; i<score2.length; i++) {
	if(score2[i] > max) {
		max = score2[i];
	}else if(score2[i] < min) {
		min = score2[i];
	}
} 

System.out.println("최대값 : " + max);
System.out.println("최소값 : " + min);

 

728x90
반응형

+ Recent posts