Java

스캐너(Scanner)를 통해 학생의 성적정보를 처리하는 기능 구현하기

에어팟맥스 2022. 6. 7. 21:31

try&catch 글 참조

package my.day05.b.scanner;

import java.util.Scanner;

public class Sungjuk {
	
	// 필드(field) 생성
	String hakbun; 
	String name; // hakbun과 name 은 null로 초기화
	int kor; 
	int eng;
	int math; // kor, eng, math 는 0으로 초기화

 

// constructor (생략되어져 있음)
	/*
    public Sungjuk() {
		
	}
	
	public Sungjuk(String hakbun, String name, int kor, int eng, int math) {
		this.hakbun = hakbun; // 필드명과 지역변수가 같으면 지역변수 우선, this. 사용
		this.name = name;
		this.kor = kor;
		this.eng = eng;
		this.math = math;
	}
	*/

 

 

아래에서 학생의 성적 정보를 "입력"해주는 메소드(registerInfo)를 생성한다.

각 학생들은 다른 사람이기 때문에 인스턴스 메소드를 사용한다.

 

// 입력 기능
void registerInfo(Scanner sc) { 
		
		int flag = 0;
		String subject = "";
		
		try {
			System.out.print("1. 학번: ");
			hakbun = sc.nextLine();
			
			System.out.print("2. 성명: ");
			name = sc.nextLine();
			
			System.out.print("3. 국어: ");
			kor = Integer.parseInt(sc.nextLine());
			flag = 1;
			
			System.out.print("4. 영어: ");
			eng = Integer.parseInt(sc.nextLine());
			flag = 2;
			
			System.out.print("5. 수학: ");
			math = Integer.parseInt(sc.nextLine());
			
		} catch(NumberFormatException e) {
			if(flag == 0) {
				subject = "국어";
			}
			else if(flag == 1) {
				subject = "영어";
			}
			else if(flag == 2) {
				subject = "수학";
			}
			System.out.println(">> "+ subject +"과목은 정수로 입력해야 합니다. <<\n");
		} // end of catch
	} // end of registerInfo

 

총점을 리턴하는 메소드 생성하기

// 총점을 리턴하는 메소드
	int total() {
		return kor+eng+math;
	} // end of total

 

평균을 리턴하는 메소드 생성하기

// 평균을 리턴하는 메소드
	double avg() {
		return total()/3.0;
	} // end of avg

 

학점을 리턴하는 메소드 생성하기

// 학점을 리턴하는 메소드
	char hakjum() {
		double avg = avg();
		
		char result = ' ';
		
		if(avg >= 90) {
			result = 'A';
		}
		else if(avg >= 80) {
			result = 'B';
		}
		else if(avg >= 70) {
			result = 'C';
		}
		else if(avg >= 60) {
			result = 'D';
		}
		else {
			result = 'F';
		}
		
		return result;
	} // end of hakjum

 

위의 registerInfo() 에서 입력받은 학생의 성적 정보와 그 정보를 통하여 구한 성적의 총합, 평균, 학점을 포함하는 

성적정보를 "출력"하는 메소드(showInfo) 생성하기

void showInfo() { // 출력 기능
		System.out.println("\n====="+ name + "님 성적정보 =====\n"
						+ "1. 학번: " +hakbun+ "\n"
						+ "2. 성명: " +name+ "\n"
						+ "3. 국어: " +kor+ "\n"
						+ "4. 영어: " +eng+ "\n"
						+ "5. 수학: " +math+ "\n"
						+ "6. 총점: " +total()+ "\n"
						+ "7. 평균: " +avg()+ "\n"
						+ "8. 학점: " +hakjum()+ "\n");
		}// end of showInfo

} // end of class Sungjuk

===========================================================================================

 

아래의 메인 메소드에서 위에서 생성한 기능들을 실행시킨다.

package my.day05.b.scanner;

import java.util.Scanner;

public class Main_Sungjuk {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in); //
		
		System.out.println(">> 첫번째 학생의 성적정보를 입력하세요 <<");
		Sungjuk iuSj = new Sungjuk(); // 아이유 학생의 성적정보
		iuSj.registerInfo(sc); 
		
		System.out.println("총점: " +iuSj.total()+ ", 평균: " +iuSj.avg()+ ", 학점: " +iuSj.hakjum());
		iuSj.showInfo();
		
		System.out.println(">> 두번째 학생의 성적정보를 입력하세요 <<");
		Sungjuk suzySj = new Sungjuk(); // 수지 학생의 성적정보
		suzySj.registerInfo(sc);
		
		System.out.println("총점: " +suzySj.total()+ ", 평균: " +suzySj.avg()+ ", 학점: " +suzySj.hakjum());
		suzySj.showInfo();
		
		sc.close();

	} // end of main

} // end of class Main_Sungjuk