5개의 점수(0~10)을 입력받아 최대점수와 최소점수를 뺀 합을 구하는 프로그램

배열을 활용해서 풀어보자

import java.util.Scanner;

public class Ex02_Quiz {
	public static void main(String[] args) {
    
    Scanner sc = new Scanner(System.in);
    int []score = new int[5];
    int max, min;
    int tot, result;
    
    // 5개의 점수 입력받기 
    // 점수는 0~10
    
    tot = 0;
    for(int i=0; i<5; i++){
    	do {
        System.out.print((i+1)+"번째 점수 : ");
    	score[i] = sc.nextInt();
    	} while (score[i]<0 || score[i]>10);
        tot+=score[i];
    }
    max = min = score[0]; 
    // 가장 처음에 입력받은 점수를 max와 min에 넣고 비교한다.
    for(int i=1; i<score.length; i++){
    	if(min>score[i]){
        	min = score[i];
        } else if(max<score[i]) {
        	max = score[i];
        }
    }
    
    result = tot - min - max;
    
    System.out.print("점수 리스트 : ");
    for(int n: score) {
    System.out.print(n+"  ");
    }
    System.out.print("\n취득 점수 : " + result);
    
    sc.close()
   
    }
}

+ Recent posts