카테고리 없음

가변길이

jay Joon 2020. 1. 26. 19:17

c나 c++은 포인터나 참조를 쓸수 있으나 java에서는 주소를 쓸수 없음으로

함수 단위에서 공유할 것이라면 기본자료형을 쓰는 것이아니라 구조체로 묶어서 공유해야 한다.

 

이렇게 두개의 값을 공유하기위해

Exam[] exams = new Exam[3];
int current=0;               

 

ExamsList새로운 클래스를 생성한다.

public class Examlist {
Exam[] exams;
int current=0;
}

다시 프로그램으로 돌아와서

public static void main(String[] args) {

Examlist list = new Examlist(); // Examlist 을 참조 하기위해 list 변수를 선언

list.exams= new Exam[3];   
list.current=0;

.

.

case 1:                                                        case 1:
    inputlist(exams,current);              ->                          inputlist(list);
    

 

private static void inputlist(Exam[] exams, int current)      ->      private static void inputlist(Examlist list)     

 

 

exams[current]=exam;                        ->        list.exams[list.current]=exam;
current++                                                  list.current++;

public class ListProgram {
	public static void main(String[] args) {
		Examlist list = new Examlist();
		list.exams= new Exam[3];
		list.current=0;
		
		boolean Keeploop =true;
		int menu;
		
		while(Keeploop) {
			
			menu=  selectmenu();
			
			switch (menu) {
			case 1:
				    inputlist(list);
					
				break;
				
			case 2:
					printlist(list);
				
				break;
			case 3:
					System.out.println("시스템을 종료합니다");
				  Keeploop=false;
	
				break;

			default:
				
			}
		}
		
	}
	private static int selectmenu() {
		Scanner sc = new Scanner(System.in);
		System.out.println("1.성적입력");
		System.out.println("2.성적출력");
		System.out.println("3.종료");
		
		int menu =sc.nextInt();
		
		return menu;
	}

	private static void inputlist(Examlist list) {
		Scanner sc = new Scanner(System.in);
		int current = list.current;
				int kor;  
				int math; 
				int eng; 	
			do {
				System.out.printf("%d번쨰국어성적을 입력하세요",current+1);
				 	kor =sc.nextInt();
				 if(kor<0||kor>100)
					 System.out.println("입력범위를 확인하세요"); 	
			} while (kor<0||kor>100);
			
			do {
				System.out.printf("%d번쨰수학성적을 입력하세요",current+1);
					math =sc.nextInt();
				 if(math<0||math>100)
					 System.out.println("입력범위를 확인하세요"); 	
			} while (math<0||math>100);
			
			do {
				System.out.printf("%d번쨰영어성적을 입력하세요",current+1);
					eng =sc.nextInt();
				if(eng<0||eng>100)
					 System.out.println("입력범위를 확인하세요"); 	
			} while (eng<0||eng>100);	
			Exam exam = new Exam();
			exam.kor=kor;
			exam.math=math;
			exam.eng=eng;
			Exam[] exams = list.exams;
				int size = list.current;
			if(exams.length ==size) {
			// 크기가 5개 더 큰 새로운 배열생성
			Exam[] temp = new Exam[size+5];																				
			for(int i=0; i<size; i++)
				temp[i]= exams[i];
		    list.exams=temp;
		    }
			list.exams[list.current]=exam;
			list.current++;
	}
	
	private static void printlist(Examlist list) {
		int total;
		float avg;
		
		int size =list.current;
		Exam[] exams=list.exams;
		for(int i=0; i<size; i++) {
			Exam exam = exams[i];
			int kor= exam.kor;
			int eng= exam.eng;
			int math= exam.math;
			
			total= kor+eng+math;
			avg=total/3.0f;
			
			System.out.printf("%d번째 성적입니다\n",i+1);
			System.out.printf("국어성적:%d\n",kor);
			System.out.printf("수학성적:%d\n",math);
			System.out.printf("영어성적:%d\n",eng);
			System.out.printf("총점:%d\n",total);
			System.out.printf("평균:%6.2f\n",avg);
			
		}		
	}
}