참고 코드
배열을 사용한 성적표 출력

학점 출력기(성적표) 메서드

조건

성적을 입력받는 함수, 예외처리, 학점 계산을 하는 메서드로 생성하여 이전처럼 성적표로 출력하는 코드를 작성하세요.

출력 방식

메서드 1개만 생성한 코드

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
import java.util.Scanner;
//메소드 1개
public class Sungjuk_Ex01_Method {

static int MAX = 3;
 
 public static void main(String[] args) {
  String[] hakbun = new String[MAX];
  String[] irum = new String[MAX];
  int[] kor = new int [MAX]; 
  int[] eng = new int[MAX];
  int[] math = new int [MAX];
  int tot[]= new int [MAX];
  double avg[] = new double [MAX];
  String grade[] = new String[MAX];
  
  int i;
  int cnt = 0;
  double avg_hap = 0;
  
  cnt = input_sungjuk(hakbun, irum, kor, eng, math, tot, avg, grade);
  
   System.out.println("\n\t\t *** 성적표 ***");
   System.out.println("===============================================================");
   System.out.println("학번   이름  국어  영어  수학  총점   평균   등급");
   System.out.println("===============================================================");
   for(i = 0; i < 3; i ++) {
    avg_hap += avg[i];
   System.out.printf("%4s   %3s   %3d   %3d   %3d   %3d   %5.2f   %2s\n",
      hakbun[i], irum[i], kor[i], eng[i], math[i], tot[i], avg[i], grade[i]);
   }
   System.out.println("===============================================================");
   System.out.printf("\t 학생수 = %d, 전체 평균 = %5.2f\n", cnt, avg_hap / cnt);
  
 }//main
 
 static int input_sungjuk(String hakbun[], String[] irum,int[] kor , int[] eng, int[] math, int tot[], double avg[], String grade[]) {
  int i;
  int cnt = 0;
  double avg_hap = 0;
  
  Scanner scan = new Scanner(System.in);
  
  for(i = 0; i < MAX; i++) {
   System.out.print("학번 입력 => ");
   hakbun[i] = scan.next();
   
   if (hakbun[i].equals("exit")) // exit 입력시 빠져나옴
    break;
   
   System.out.print("이름 입력 => ");
   irum[i] = scan.next();
   
   while(true) {
        try {
            System.out.print("국어점수 입력 => ");
            kor[i] = scan.nextInt();
        }
        catch(Exception e) {
            System.out.println("국어점수 입력 오류 => " + e.getMessage());
            if(scan.hasNext()) //버퍼로부터 읽을 데이터가 있는지 확인하는 메소드 (true, false 반환)
            scan.next(); //잘 못 입력한 점수 비우기
            
            continue;
        }
        break;
   }

   System.out.print("영어점수 입력 => ");
   eng[i] = scan.nextInt();
   System.out.print("수학점수 입력 => ");
   math[i] = scan.nextInt();
   
   tot[i] += kor[i] + eng[i] + math[i];
   avg[i] = tot[i] / 3;
   
   switch ((int)avg[i] / 10) {
    case 10:
    case 9:
        grade[i] = "수";
        break;
        
    case 8: 
        grade[i] = "우";
        break;
    case 7:
        grade[i] = "미";
        break;
    case 6:
        grade[i] = "양";
        break;
    default: 
        grade[i] = "가";
        break;
   }
   cnt++; //입력 카운트 증가
   System.out.println();
  }//for
  return cnt;

 }
}

기능들을 모두 메서드로 변경한 코드

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
import java.util.Scanner;
//모두 메소드
public class Sungjuk_Ex01_Method {

static int MAX = 3;
 
 public static void main(String[] args) {
  String[] hakbun = new String[MAX];
  String[] irum = new String[MAX];
  int[] kor = new int [MAX]; 
  int[] eng = new int[MAX];
  int[] math = new int [MAX];
  int tot[]= new int [MAX];
  double avg[] = new double [MAX];
  String grade[] = new String[MAX];
  int cnt = 0;
  
  //cnt = 입력받은 갯수 가지고 있음
  cnt = input_sungjuk(hakbun, irum, kor, eng, math, tot, avg, grade);
  output_sungjuk(hakbun, irum, kor, eng, math, tot, avg, grade, cnt);
   
  
 }
 
 static int input_sungjuk(String hakbun[], String[] irum,int[] kor , int[] eng, int[] math, int tot[], double avg[], String grade[]) {
  int i;
  int cnt = 0;
  double avg_hap = 0;
  
  Scanner scan = new Scanner(System.in);
  
  
  for(i = 0; i < MAX; i++) {
   System.out.print("학번 입력 => ");
   hakbun[i] = scan.next();
   
   if (hakbun[i].equals("exit")) // exit 입력시 빠져나옴
    break;
   
   System.out.print("이름 입력 => ");
   irum[i] = scan.next();
   
   while(true) {
        try {
            System.out.print("국어점수 입력 => ");
            kor[i] = scan.nextInt();
        }
        catch(Exception e) {
            System.out.println("국어점수 입력 오류 => " + e.getMessage());
            if(scan.hasNext()) //버퍼로부터 읽을 데이터가 있는지 확인하는 메소드 (true, false 반환)
            scan.next(); //잘 못 입력한 점수 비우기
            
            continue;
        }
    break;
   }

   System.out.print("영어점수 입력 => ");
   eng[i] = scan.nextInt();
   System.out.print("수학점수 입력 => ");
   math[i] = scan.nextInt();
   
   tot[i] += kor[i] + eng[i] + math[i];
   avg[i] = tot[i] / 3;
   
   switch ((int)avg[i] / 10) {
    case 10:
    case 9:
        grade[i] = "수";
        break;
        
    case 8: 
        grade[i] = "우";
        break;
    case 7:
        grade[i] = "미";
        break;
    case 6:
        grade[i] = "양";
        break;
    default: 
        grade[i] = "가";
        break;
   }
   cnt++; //입력 카운트 증가
   System.out.println();
  }//for
  return cnt;
 }
 
 //파라미터 값을 받을 때 추가할 것 -> int cnt : 입력받은 실제 갯수도 받아야 함
 static void output_sungjuk(String hakbun[], String[] irum,int[] kor , int[] eng, int[] math, int tot[], 
   double avg[], String grade[], int cnt) {
  
    int i;
    double avg_hap = 0;
    
    System.out.println("\n\t\t *** 성적표 ***");
    System.out.println("===============================================================");
    System.out.println("학번   이름  국어  영어  수학  총점   평균   등급");
    System.out.println("===============================================================");
    for(i = 0; i < 3; i ++) {
        avg_hap += avg[i];
        System.out.printf("%4s   %3s   %3d   %3d   %3d   %3d   %5.2f   %2s\n",
            hakbun[i], irum[i], kor[i], eng[i], math[i], tot[i], avg[i], grade[i]);
    }
    System.out.println("===============================================================");
    System.out.printf("\t 학생수 = %d, 전체 평균 = %5.2f\n", cnt, avg_hap / cnt);
 }

}

🌞 정보 : 공부 기록용 블로그입니다. 오타나 내용 오류가 있을 경우 알려주시면 감사하겠습니다.

태그:

카테고리:

업데이트:

댓글남기기