프로그래밍-학습기록/코딩테스트
백준 온라인 저지 | 10039 | 실습: 평균점수
leesche
2020. 7. 19. 16:57
나의 풀이
한 줄에 여러 값을 입력 받는 것이 아니라서 bufferedReader를 쓰지 않았다(그냥 빨리 풀고 싶었다). scores 라는 크기 5개 짜리 배열을 만들어 차례로 점수를 집어넣었고, 넣고 난 다음 바로 점수 크기 검사를 하여 40점 미만이면 40점으로 점수를 바꿨다. 반복문 안에서 동시에 점수 총합을 계산한 다음 출력할 때 scores 배열의 크기(5)로 나누어 평균 점수를 구했다.
package baekjunOnlineJudge;
import java.util.Scanner;
public class Bj_10039 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int scores[] = new int[5];
int sum = 0;
int len = scores.length;
for(int i=0; i < len; i++) {
scores[i] = sc.nextInt();
if(scores[i]<40) {
scores[i] = 40;
}
sum += scores[i];
}
System.out.print(sum/len);
}
}