Exam[] exams = new Exam[3] ; -> 이름표 3개만 만들어준것이다.

exams[0] = new Exam(); -> 실제 객체에 참조를 꼭 시켜줘야한다. 반드시생성!

exams[1] = new Exam();

exams[3] = new Exam();

-> 각각 만들어 줘야한다.

import java.util.Scanner;

public class Program {

	public static void main(String[] args) {

		Exam[] exams = new Exam[3];
		
		int menu;
		boolean keepLoop = true;
		
		while(keepLoop)
		{
			 menu =inputMenu();
			switch(menu)
			{
			case 1:
				inputList(exams);
				break;
			case 2:
				printList(exams);
				break;
			case 3:
				System.out.println("Bye~~");
				keepLoop = false;
				break;
				
			default:
				System.out.println("잘못된 값을 입력하셨습니다. 메뉴는 1~3까지입니다.");
			}
		}
		
		exams[0] = new Exam(); 
		exams[0].kor = 30;
		
		System.out.printf("kor:%d", exams[0].kor);
		
	}
		private static void printList(Exam[] exams) {
			System.out.println("┌──────────────────┐");
			System.out.println("│     성적 출력             │");
			System.out.println("└──────────────────┘");
			System.out.println();
			
			for(int i= 0 ; i<3; i++ ) {
				Exam exam = exams[i];
				int kor = exam.kor;
				int eng = exam.eng;
				int math = exam.math;
				
				int total = kor+eng+math; 
				float avg= total/3.0f;
				
				System.out.printf("국어 :%d\n", kor);
				System.out.printf("영어 :%d\n", eng);
				System.out.printf("수학 :%d\n", math);
				
				System.out.printf("총점 : %3d\n", total);
				System.out.printf("평균 : %6.2f\n", avg);
				System.out.println("────────────────────────");
			}
	}
		
		
		private static void inputList(Exam[] exams) {
			Scanner scan = new Scanner(System.in);
			System.out.println("┌──────────────────┐");
			System.out.println("│     성적 입력             │");
			System.out.println("└──────────────────┘");
			System.out.println();
			
			for(int i=0 ; i<3; i++) {
				int kor, eng, math;
				
					do {
						System.out.printf("국어 :");
						kor = scan.nextInt();
						
						if(kor < 0 || 100 < kor)
						{
							System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
						}
					}while(kor<0 || 100 < kor);
					
					do {
						System.out.printf("영어 :");
						eng = scan.nextInt();
						
						if(eng < 0 || 100 < eng)
						{
							System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
						}
					}while(eng<0 || 100 < eng);
					
					do {
						System.out.printf("수학 :");
						math = scan.nextInt();
						
						if(math < 0 || 100 < math)
						{
							System.out.println("국어성적은 0~100까지의 범위만 입력이 가능합니다.");
						}
					}while(math<0 || 100 < math);
					Exam exam = new Exam();
					exam.kor=kor;
					exam.eng=eng;
					exam.math=math;
					
					exams[i] = exam;   // 꼭해줘야하는 작업
			}
		
			
			System.out.println("────────────────────────");
	}
		static int inputMenu() {
			Scanner scan = new Scanner(System.in);
			System.out.println("┌──────────────────┐");
			System.out.println("│     메인 메뉴             │");
			System.out.println("└──────────────────┘");
			System.out.println("\t1. 성적입력 ");
			System.out.println("\t2. 성적출력 ");
			System.out.println("\t3. 종료 ");
			System.out.print("\t선택> ");
			int menu = scan.nextInt();
			
			return menu;
		}
	}
  • 반복해서 볼것

참고 : https://youtu.be/j9cYw2gfES0

댓글남기기