본문 바로가기
스마트인재개발원/자바

[객체 배열] 배열에 담아 학생의 점수 합계,평균 구하기

by 죠졍니 2022. 9. 22.
728x90
반응형
SMALL

 

 

 


package 오후;

public class StudentScore {

	private String name;
	private int scoreJava;
	private int scoreWeb;
	private int scoreAndroid;
	
	
	public StudentScore(String name, int scoreJava, int scoreWeb, int scoreAndroid) {
		super();
		this.name = name;
		this.scoreJava = scoreJava;
		this.scoreWeb = scoreWeb;
		this.scoreAndroid = scoreAndroid;
	}


	public String getName() {
		return name;
	}


	public int getScoreJava() {
		return scoreJava;
	}


	public int getScoreWeb() {
		return scoreWeb;
	}


	public int getScoreAndroid() {
		return scoreAndroid;
	}
	
	
	
	
}

 

 

 

 

 

package 오후;

import java.util.Scanner;

public class StudentScoreMain {

	public static void main(String[] args) {

		//StudentScore객체 배열에 대한 레퍼런스 변수 선언
		StudentScore[] ssArray;
		
		//3명의 학생 데이터를 입력할 수 있도록 StudentScore객체 배열 생성
		ssArray = new StudentScore[3];
		
//		StudentScore ss1;
//		StudentScore ss2;
//		StudentScore ss3;

		Scanner sc = new Scanner(System.in);
		
		for(int i=0;i<ssArray.length;i++) {
			System.out.print(i+1 + "번째 학생의 이름을 입력하세요. >>");
			String name = sc.next();
			System.out.print(i+1 + "번째 학생의 Java점수를 입력하세요. >>");
			int scoreJava = sc.nextInt();
			System.out.print(i+1 + "번째 학생의 Web점수를 입력하세요. >>");
			int scoreWeb = sc.nextInt();
			System.out.print(i+1 +"번째 학생의 Android점수를 입력하세요. >>");
			int scoreAndroid = sc.nextInt();
			
			System.out.println();
			
			ssArray[i]=new StudentScore(name, scoreJava, scoreWeb, scoreAndroid);
			
		}
	
		for(int i=0;i<ssArray.length;i++) {
			System.out.println(ssArray[i].getName()+"님의 총점은 "+ 
			(ssArray[i].getScoreJava()+ssArray[i].getScoreWeb()+ssArray[i].getScoreAndroid())
			+ "점이고, 평균은 "
			+ (ssArray[i].getScoreJava()+ssArray[i].getScoreWeb()+ssArray[i].getScoreAndroid())/3
			+ "점 입니다."
			);
		}
		
	}    

}
728x90
반응형
LIST