본문 바로가기

카테고리 없음

배열 총점, 평점 내기

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
package day_06;
 
public class Day06Ex02 {
 
    public static void main(String[] args) {
        /*
        배열을 선언과 동시에 초기화한다. (배열 리터럴 사용)
        int a[];  // 대괄호가 뒤에 붙어도 배열타입.
        int[] a= {85,90,75,100,95};
        
        참조변수 선언
        리터럴은 초기화 할때만 사용이 가능하다.
        int[] a;
        a= {85,80,75,100,95}; ERROR
        
        
        new생성자를 이용하면 가능하다.
        생성 후 값을 넣는방식. > 함수의 인자로 배열을 초기화할때(리터럴을 사용할때)도 사용된다. 
        int[] a;
        a= new int[]{85,80,75,100,95}; 
        
        main함수의 매개변수는 배열이다.
        main(new String[]{"kim","서울","프로그래머"});
    
        위 방법을 모른다면 미리 선언하도록 하자. 그러나 두 번 선언해야함. 위 리터럴을 사용하면 한줄로 가능.
        String[] strArr= {"kim","서울","프로그래머"};
        main(staArr); 
        */
        
        int[] scoreArr= null;
        int total= 0;     //총점
        double avg= 0;        //평점
        
        scoreArr= new int[]{85,90,75,100,95};
        
        for(int i=0; i<scoreArr.length; i++) {
            total += scoreArr[i];
        }
        
        avg= total/scoreArr.length;
        
        System.out.printf("total: " + total + " avg: " + avg);
            
        
    
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs