Java/배열

배열(6)

은찡안찡 2023. 4. 3. 16:08
package com.dream.array;

import java.util.Scanner;

//배열을 사용해 학생의 점수 총점과 평균을 출력하라.
public class ArrayEx06 {
	public static void main(String[] args) {
		//변수 선언
		Scanner scan = new Scanner(System.in);
		double total = 0, avg = 0;
		int studentNum = 0;
		
		System.out.println("학생 수를 입력하세요.");
		System.out.print("입력: ");
		studentNum = scan.nextInt( );
		int[] score = new int[studentNum];
		
		for(int i=0; i<studentNum; i++) {
			System.out.print("점수 입력: ");
			score[i] = scan.nextInt();
		}
		
		//총점을 구한다.
		for(int i=0; i<score.length; i++) {
			total += score[i];
		}
		//평균을 구한다.
		avg = total / score.length;
		//총점과 평균을 출력한다.
		System.out.println("총점: "+total);
		System.out.println("평균: "+avg);
	}
}

'Java > 배열' 카테고리의 다른 글

배열(8)  (0) 2023.04.03
배열(7)  (0) 2023.04.03
배열(5)  (0) 2023.04.03
배열(4)  (0) 2022.11.11
배열(3)  (0) 2022.11.11