카테고리 없음

7일차//[JAVA] 2차원배열, 학생 성적 입력 - > 합계, 평균 출력

aesup 2021. 1. 20. 11:29
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.Arrays;
import java.util.Scanner;
 
public class MainClass {
 
    public static void main(String[] args) {
 
        /*
         * 학생성적관리 몇명? <- 입력 //2차원배열 과목(이름,국어, 영어, 수학) 총점 평균 수학점수 최고점 영어 점수의 최하점
         * 
         * 
         * 총점 평균 수학점수 최고점수 영어점수 최하점수
         */
 
        Scanner sc = new Scanner(System.in);
 
        // String student [][] = null;
        // 명수 입력
        //System.out.print("몇명의 학생의 데이터를 등록하시겠습니까? = ");
        //int count = sc.nextInt();
 
        //String student[][] = new String[count][4];
 
        String student[][] = {
                
                {"홍길동","100","80","75"},
                {"일지매","90","90","75"},
                {"성춘향","100","80","75"},
                {"장옥정","100","80","75"},
                {"정수동","85","75","90"}
                
                
                
        };
        
        
        String title[] = { "이름""국어""영어""수학" };
 
        // 데이터 입력을 받을차례
        
        
        /*
        
        for (int i = 0; i < student.length; i++) {
            
             * System.out.println("이름  = "); String name = sc.next(); student[i][0] = name;
             * 
             * System.out.println("국어 = "); String lang = sc.next(); student[i][1] = lang;
             * System.out.println("영어 = "); String eng = sc.next(); student[i][2] = eng;
             * System.out.println("수학 = "); String math = sc.next(); student[i][3] = math;
             
            
            for (int j = 0; j < title.length; j++) {//과목별로 for문을 돌려 편하게 한다.
                System.out.print(title[j] + " = ");
                String colum = sc.next();
 
                student[i][j] = colum;
 
            }
        }
        */
            
        
 
            for (int i = 0; i < student.length; i++) {
                System.out.println(Arrays.toString(student[i]));
 
            }
            
            
            
            //총점
            
            int sum = 0;
            
            for (int i = 0; i < student.length; i++) {
                for (int j = 1; j < student[i].length; j++) { // 0은 이름이니까 1부터 시작
                    sum = sum + Integer.parseInt(student[i][j]);
                    
                }
                
            }
            
            System.out.println("총점 = "+ sum);
            
            //데이터 탐색
            String findName = "이수빈";// 찾고자하는 사람
            int index = 0;
            for (int i = 0; i < student.length; i++) {
                if(student[i][0].contentEquals(findName)) {
                    index = i;
                    break;
                }
                    
                
            }
            System.out.println("index" + index);
            
            
            
            //평균
            double avg = 0;
            if(student.length > 0) {
             avg = sum / student.length;
            }
            System.out.println("평균  = " + avg);
            
            
            
            //수학점수 최고점수
            int max = 0 ;
            int humanIndex = 0;
            for (int i = 0; i < student.length; i++) {
                int math = Integer.parseInt(student[i][3]);
                if(max < math) {
                    max = math;
                    humanIndex = i;
                    
                }
                
            }
            System.out.println("1등은"+student[humanIndex][0+ "며 점수는"+ max + "입니다");
            
            
            
            
            
            
            
            
            
            
 
        
 
    }
}
cs

 

728x90