명품 JAVA 프로그래밍

명품 JAVA 프로그래밍 7장 실습문제 5

anycoding 2021. 12. 1. 20:38
반응형

문제

하나의 학생 정보는 Student 클래스로 표현한다. Student 클래스에는 이름, 학과, 학번, 학점 평균을 나타내는 필드가 있다. 키보드로 학생 정보를 5개 입력받아 ArrayList<Student>에 저장한 후에 ArrayList<Student>의 모든 학생 정보를 출력하는 프로그램을 작성하라.

 

소스 코드

import java.util.*;
class Student{
	private String name; // 이름
	private String Department; // 학과
	private int StudentID; // 학번
	private double Score; // 학점
	Student(String name, String Department,int StudentID, double Score){
		this.name = name;
		this.Department = Department;
		this.StudentID = StudentID;
		this.Score = Score;
	}
	public void Print(){
		System.out.println("학생 이름 : "+name);
		System.out.println("학과 이름 : "+Department);
		System.out.println("학번 : "+StudentID);
		System.out.println("학점 : "+Score);
	}
	
}
public class Main5 {
	public static void main(String[] args)
	{
		ArrayList<Student> student = new ArrayList<Student>();
		Scanner s = new Scanner(System.in);
		System.out.println("학생 5명의 정보를 입력하시오");
		for(int i = 0;i<5;i++)
		{
			System.out.print("학생 이름 : ");
			String name = s.nextLine();
			System.out.print("학과 이름 : ");
			String department = s.nextLine();
			System.out.print("학번 : ");
			int studentID = s.nextInt();
			System.out.print("학점 : ");
			double score = s.nextDouble();
			s.nextLine();
			Student temp = new Student(name,department,studentID,score);
			student.add(temp);
		}
		Iterator<Student> it = student.iterator();
		int i = 1;
		while(it.hasNext()) {
        	System.out.println();
			System.out.println(i+"번 학생");
			it.next().Print();
			i++;
		}
        s.close();
	}
}

결과

학생 5명의 정보를 입력하시오
학생 이름 : 유관순
학과 이름 : 독립
학번 : 19021216
학점 : 4.5
학생 이름 : 이순신
학과 이름 : 무예
학번 : 15450428
학점 : 4.5
학생 이름 : 세종대왕
학과 이름 : 훈민정음
학번 : 13970515
학점 : 4.5
학생 이름 : 백범 김구
학과 이름 : 독립
학번 : 18760829
학점 : 4.5
학생 이름 : 안중근
학과 이름 : 독립
학번 : 18790902
학점 : 4.5

1번 학생
학생 이름 : 유관순
학과 이름 : 독립
학번 : 19021216
학점 : 4.5

2번 학생
학생 이름 : 이순신
학과 이름 : 무예
학번 : 15450428
학점 : 4.5

3번 학생
학생 이름 : 세종대왕
학과 이름 : 훈민정음
학번 : 13970515
학점 : 4.5

4번 학생
학생 이름 : 백범 김구
학과 이름 : 독립
학번 : 18760829
학점 : 4.5

5번 학생
학생 이름 : 안중근
학과 이름 : 독립
학번 : 18790902
학점 : 4.5
반응형