Java 코드연습

[뉴렉처] 구조체 배열 이용하기

양상추상츄 2021. 10. 18. 15:02

https://www.youtube.com/watch?v=j9cYw2gfES0&list=PLq8wAnVUcTFWQ4TpRPZRa5nj1VwfyO7st&index=10 

좋은강의

 

배열이란 애는 이름표만 준비해둔거

오류가 안나려면 실제 Exam(); 객체를 참조해줘야함

 

package Exam;

public class Exam {
	
	int kor;
	int eng;
	int math;
	
}
package Exam;

import java.util.Scanner;

public class ExamProgram {
	
	static Scanner scan = new Scanner(System.in);
	
	public static void main(String[] args) {
		
		Exam[] exams = new Exam[3]; 
        //메인 메소드에서 배열 생성
        //이 안에 Exam(); 객체생성
		
		//메뉴
		int menu = 0; // 1.INPUT 2.PRINT 3.EXIT
		final int MENU_INPUT = 1; // 상수
		final int MENU_PRINT = 2; // 상수
		final int MENU_EXIT = 3; // 상수
		boolean loop = true;
		
		//메뉴
		while(loop){
			System.out.println("\t1.성적입력");
			System.out.println("\t2.성적출력");
			System.out.println("\t3.종료");
			System.out.print("\t>>>");
			
			menu = scan.nextInt();
			
			switch(menu) {
			
			case MENU_INPUT://1
				input(exams); // exams배열을 매개변수로 받음
				
			case MENU_PRINT://2
				print(exams);// exams배열을 매개변수로 받음

			case MENU_EXIT://3
				//EXIT
				System.out.println("good bye~");
				break;
			
			default:
				System.out.println("입력오류 : 1에서 3까지만 입력 할 수 있습니다.");
				
			}
			break;
		}
		
	}

	private static void print(Exam[] exams) { 
    //Exam이란 배열에 exams라고 이름붙인걸 가져오겠다
		
		for(int i=0; i<3; i++) {
			
			int total;
			float avg;
			
			int kor = exams[i].kor;
			int eng = exams[i].eng;
			int math = exams[i].math;
			
			total = kor + eng + math;
			avg = total / 3.0f;
			
			System.out.println("------------------------------------");
			System.out.println("@@@@@@@@@@@@@성적출력@@@@@@@@@@@@@@@@");
			
			System.out.printf("\t국어%d: %3d\n",i+1 ,kor);// \t 탭, %3d 3칸 정렬, \n 한칸내리기
			System.out.printf("\t영어%d: %3d\n",i+1 ,eng);
			System.out.printf("\t수학%d: %3d\n",i+1 ,math);
			
			
			System.out.printf("\t총점: %3d\n", total); // 3칸 정렬
			System.out.printf("\t평균: %6.2f\n", avg); // 소수점 2자리 까지 나오고 6칸 정렬
			System.out.println("------------------------------------");
		}
		
	}

	private static void input(Exam[] exams) {
		
		for(int i=0; i<3; i++) {
			System.out.println("------------------------------------");
			System.out.println("@@@@@@@@@@@@@성적입력@@@@@@@@@@@@@@@@");
			
			exams[i] = new Exam(); 
            //중요!! Exam();객체생성해서 배열에 넣음 방의 개수는 3
			
			int kor = exams[i].kor;
			int eng = exams[i].eng;
			int math = exams[i].math;
			
			do{
				System.out.printf("국어%d : ",i+1);
				kor=scan.nextInt();//점수입력
				
				if(kor<0||100<kor) {
					System.out.println("성적범위를 벗어났습니다.");
				}
			}while(kor<0||100<kor); // 점수 범위를 벗어날 경우 false, 위의 내용 다시 반복
			exams[i].kor = kor;
			
			do{
				System.out.printf("영어%d : ",i+1);
				eng=scan.nextInt();//점수입력
				
				if(eng<0||100<eng) {
					System.out.println("성적범위를 벗어났습니다.");
				}
			}while(eng<0||100<eng); // 점수 범위를 벗어날 경우 false, 위의 내용 다시 반복
			exams[i].eng = eng;
			
			do{
				System.out.printf("수학%d : ",i+1);
				math=scan.nextInt();//점수입력
				
				if(math<0||100<math) {
					System.out.println("성적범위를 벗어났습니다.");
				}
			}while(math<0||100<math); // 점수 범위를 벗어날 경우 false, 위의 내용 다시 반복
			exams[i].math = math;
		}
		

	}

}