[Java] - 혼자공부하는자바 200~202p. 확인문제

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
package chapter05;
import java.util.Scanner;
 
public class Excer04 {
 
    public static void main(String[] args) {
        //확인문제 4번
        int max = 0;
        int [] arr = {1,5,3,8,2,3,7,3,6,10};
        
        for (int i = 0; i <10; i++) {
            if(max<arr[i]) {
                max = arr[i];
            }
        }
        System.out.println(max);
        
        
        // 확인문제 3번,5번
        int [][]  arr2 = {
                {95,86},
                {83,92,96},
                {78,89,93,87,88}
        };
        
        int count = 0;
        double avg = 0;
        int sum = 0;
        
        for(int i =0; i<arr2.length; i++) {
            forint j=0; j<arr2[i].length; j++) {
                sum += arr2[i][j];
                count++;
            }
        }
        System.out.println("arr2의 길이 : "+arr2.length+",\t arr2[2]의 길이:"+arr2[2].length);// 3번 답
        avg = (double)sum/count;
        System.out.println("평균:"+avg); // 5번 답
        
        //확인문제 6번
        Scanner sc = new Scanner(System.in);
        boolean run = true;
        int student_num = 0;
        int scores [] = null;
        int max_score = 0;
        double avg_score= 0;
        int sum2 =0;
        int no_2_count =0;
    
        
        while(run) {
            System.out.println("----------------------------------------------");
            System.out.println("1.학생수 | 2.점수입력 | 3.점수리스트 | 4.분석 | 5. 종료");
            System.out.print("선택");
            int select_no = Integer.parseInt(sc.nextLine());
            
            if( select_no ==1) {
                System.out.print("학생수:");
                student_num = Integer.parseInt(sc.nextLine());
                scores = new int[student_num];
            } else if(select_no ==2){
                if(student_num < 1) {
                    System.out.println("학생수를 입력하지 않으셨습니다.");
                } else {
                    for(int i=0; i<student_num; i++) {
                        System.out.print("scores["+i+"]1의 점수를 입력하세요.");
                        scores[i] = Integer.parseInt(sc.nextLine());
                        no_2_count++;
                    }
                }
            } else if(select_no ==3) {
                if(student_num <1) {
                    System.out.println("학생수를 입력하지 않으셨습니다.");
                } else if(no_2_count<1) {
                    System.out.println("학생점수를 입력하지 않으셨습니다.");
                } else {
                    for(int i=0; i<student_num; i++) {
                        System.out.println(scores[i]);
                    }
                }
            } else if(select_no ==4) {
                if(student_num <1 || scores.length <0) {
                    System.out.println("학생수와 학생 점수가 입력되지 않았습니다.");
                } else {
                        for(int i =0; i<student_num; i++) {
                            if(max<scores[i]) {
                                max = scores[i];
                            }
                            sum2 += scores[i];
                        }
                        avg = (double)sum2/student_num;
                        System.out.println("최고 점수 : " + max);
                        System.out.println("평균 점수 : " + avg);
                        System.out.println("총점수 :"+sum2);
                }
            } else if (select_no ==5) {
                run = false;
                System.out.println("프로그램이 종료됩니다.");
            }            
        }
    }
 
}
 
cs

 

-6번 출력결과-