#BufferedReader #Array

 

 public static void main(String[] args) throws NumberFormatException, IOException {
  
  // 국어, 영어, 수학 점수 입력 받아 총점 평균 구하기
  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  
  
  String[] subject = {"국어", "영어", "수학"};
  int[] score = new int[subject.length +1]; // 총점까지 계산하기 위해 길이값을 +1 해준다.
  
  for(int i =0; i<subject.length; i++) {
   do {
    System.out.print(subject[i] + " : ");
    score[i] = Integer.parseInt(reader.readLine());
   }while(score[i] < 0 || score[i] > 100); // score 값이 0보다 작거나 또는, 100보다 큰 경우 다시 입력받는다.


   // 총점 score[score.length -1] -- 마지막 포인트
   score[score.length-1] += score[i];
  }
  
  // 평균
  float avg = score[score.length-1] / (float)subject.length;
  
  System.out.println("======= 기말고사 성적 =======");
  for(int i=0; i<subject.length; i++) {
   System.out.print(subject[i] + "\t");
  }
  System.out.println("총점\t 평균");
  for(int i = 0; i<score.length; i++) {
   System.out.printf("%d\t",score[i]);
  }
  System.out.printf("%.2f",avg);
 }

+ Recent posts