참고 코드
배열을 사용한 성적표 출력
성적 클래스와 메인 메소드 배열

학점 출력기(성적표) (3) - 실행 클래스 코드의 입력과 출력 기능을 input_sungjuk(), output_sungjuk() 메서드로 변경

조건

이전에 작성한 실행 클래스 코드의 입력과 출력 기능를 각각 메소드로 변경하기
성적 클래스의 코드는 그대로 사용한다.

성적(Sungjuk) 클래스 구조

  • 필드 : 학번, 이름, 국어, 영어, 수학, 총점, 평균, 등급
  • 메소드 : 성적입력(), 성적계산() - 총점, 평균, 등급계산 , 성적 출력()
  • 메인 메소드 : 총 학생 수와 전체 평균을 구해서 출력

실행 클래스

  • (추가) 메소드 : input_sungjuk(), output_sungjuk()

출력 형식

  • 아래의 코드를 input_sungjuk() 메소드로 변경

    1
    2
    3
    4
    5
    6
    7
    8
    
      for(int i =0; i < MAX; i++) {
          obj[i] = new Sungjuk();
          if(obj[i].input_Score())
              break;
          obj[i].process_Score();
          cnt++;
          System.out.println();
      }
    
  • 아래의 코드를 output_sungjuk() 메소드로 변경

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
      System.out.println("\n\t\t *** 성적표 ***");
      System.out.println("===============================================================");
      System.out.println("학번 이름  국어  영어  수학 총점   평균    등급");
      System.out.println("===============================================================");
      //obj.output_Score();
      for (int i = 0; i < cnt; i++) {
          obj[i].output_Score();
          avg_hap += obj[i].avg;
      }
        
      System.out.println("===============================================================");
      System.out.printf("\t 총 학생수 = %d, 전체 평균 = %5.2f\n", cnt, avg_hap / cnt);
    

코드

  • Sungjuk.java
    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
    
      import java.util.Scanner;
    
      public class Sungjuk {
          `String hakbun, irum;
          int kor, eng, math;
            
          int tot = 0;
          double avg = 0;
          String grade;
            
          Sungjuk() {
            
          }
            
          boolean input_Score() { //true or false
              Scanner scan = new Scanner(System.in);
                
              System.out.println("학번 입력 => ");
              this.hakbun = scan.next();
                
              if(hakbun.equals("exit"))
              return true;
                
              System.out.println("이름 입력 => ");
              this.irum = scan.next();
              System.out.println("국어 입력 => ");
              this.kor = scan.nextInt();
              System.out.println("영어 입력 => ");
              this.eng = scan.nextInt();
              System.out.println("수학 입력 => ");
              this.math = scan.nextInt();
              return false;
          }
            
          void process_Score() {
              tot += kor + eng + math;
              avg = tot / 3;
                
              switch ((int)avg / 10) {
                  case 10:
                  case 9:
                  grade = "수";
                  break;
                    
                  case 8: 
                  grade = "우";
                  break;
                  case 7:
                  grade = "미";
                  break;
                  case 6:
                  grade = "양";
                  break;
                  default: 
                  grade = "가";
                  break;
              }
            
          }
            
          void output_Score() {
              System.out.printf("%4s   %3s   %3d   %3d   %3d   %3d   %5.2f   %2s\n",
                  hakbun, irum, kor, eng, math, tot, avg, grade);
          }
      }
    
  • ExampleSungjuk.java

    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
    
      public class ExampleSungjuk_method {
        
      static int MAX = 100;
      static Sungjuk obj[] = new Sungjuk[MAX];
        
          public static void main(String[] args) {     
          int cnt = 0;
          double avg_hap = 0;
            
          cnt = input_sungjuk(obj);
          output_sungjuk(obj, cnt);
          }
            
          static int input_sungjuk(Sungjuk obj[]) {
              int cnt = 0;
                
              for(int i =0; i < MAX; i++) {
                  obj[i] = new Sungjuk();
                  if(obj[i].input_Score())
                      break;
                  obj[i].process_Score();
                  cnt++;
                  System.out.println();
              } 
                  return cnt;
          }
            
          //파라미터 값을 받을 때 추가할 것 -> int cnt : 입력받은 실제 갯수도 받아야 함
          static void output_sungjuk(Sungjuk obj[], int cnt) {
            
          double avg_hap = 0.0;
            
          System.out.println("\n\t\t *** 성적표 ***");
          System.out.println("===============================================================");
          System.out.println("학번 이름  국어  영어  수학 총점   평균    등급");
          System.out.println("===============================================================");
    
          for (int i = 0; i < cnt; i++) {
          obj[i].output_Score();
          avg_hap += obj[i].avg;
          }
            
          System.out.println("===============================================================");
          System.out.printf("\t 총 학생수 = %d, 전체 평균 = %5.2f\n", cnt, avg_hap / cnt);
    
          }
      } 
    

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

태그:

카테고리:

업데이트:

댓글남기기