반응형

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package SecondHW;
import java.util.*//Arraylist와 스캐너 사용 위한 임포트
class Student{ //Student 클래스, 스트링형 name, department, 정수형 number, double형 score 포함
    String name;
    String department;
    int number;
    double score;
    public Student(String name, String department, int number, Double score){
        this.name=name; //Studnet 클래스의 생성자 인자를 객체의 각 원소에 대입한다.
        this.department=department;
        this.number=number;
        this.score=score;
    }}
public class Roaster {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);  //스캐너 객체
        ArrayList<Student> st = new ArrayList<Student>(); //Student를 원소로 갖는 어레이리스트
        System.out.println("학생 정보를 입력하세요: ");
        for(int i=0;i<5;i++//5번 반복
        {
                System.out.print((i+1)+"이름>>");
                String name=s.nextLine();
                System.out.print((i+1)+"학과>>");
                String department=s.nextLine();
                System.out.print((i+1)+"학번>>");
                int number=s.nextInt();
                System.out.print((i+1)+"학점평균>>");
                double score=s.nextDouble();  //이름, 학과, 학번, 학점평균을 Scanner객체 s를 이용하여 받는다.
                Student tmp = new Student(name,department,number,score); //Student 객체 tmp 생성 
                st.add(tmp); //tmp를 st의 원소로 추가
                s.nextLine(); //버퍼에서 개행문자를 비워주기 위함
        }
        for(int i=0;i<st.size();i++){ //st.size=5이므로 5번 반복
            Student res = st.get(i); //어레이리스트 st에서 i번째 인덱스 원소를 res에 대입
            System.out.println("=============================");
            System.out.println("이름: "+res.name);
            System.out.println("학과: "+res.department);
            System.out.println("학번: "+res.number);
            System.out.println("학점평균: "+res.score);
            System.out.println("=============================");//각 원소 출력
        }}}
 
cs




반응형

'컴퓨터 & 프로그래밍 & 전자공학 > JAVA' 카테고리의 다른 글

제네릭 클래스  (0) 2017.06.19
학점 계산  (0) 2017.06.19
가장 큰 수  (0) 2017.06.19
겜블링 게임  (0) 2017.06.19
가위바위보  (0) 2017.06.19
반응형

7.2>Scanner 클래스를 사용하여 5개 학점(‘A’, ‘B’, ‘C’, ‘D’, ‘F’)을 문자로 입력 받아 ArrayList에 저장한다. 그리고 ArrayList를 검색하여 학점을 점수(A=4.0, B=3.0, C=2.0, D=1.0, F=0)로 변환하고 평균을 계산하여 그 결과를 출력하는 프로그램을 작성하라.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package SecondHW;
import java.util.*//scanner와 vector 사용을 위한 임포트
public class Grades {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in); //스캐너 객체와 레퍼런스 변수 생성
        ArrayList<Character> grade= new ArrayList<Character>(); //ArrayList 객체와 레퍼런스 변수 생성, 스레드 동기화 지원 안함
        double sum=0//실수 변수 sum 초기화 0
        int count=0//정수 변수 count 초기화 0
        double score[]=new double[5]; //실수형 배열 크기 5 score
        System.out.print("5개의 학점을 빈 칸으로 분리하여 입력하세요. (A/B/C/D/F)");
        while(count!=5//count가 5가 아닐때까지 반복, for를 쓰지 않은 이유는 A,B,C,D,F 이외의 것을 걸러내야해서 continue써야하는데
            //for 에서 continue 쓰면 증감식은 그대로 실행되어버리니까
        {
            String st = s.next(); //String에 문자열 입력
            char c = st.charAt(0); //지정된 index에 있는 문자 리턴해서 c에 대입
            if((c>='A'&&c<='D')||c=='F'//c가 A~D 혹은 F이면
            {grade.add(c); count++;} //grade에 c를 추가하고 count 증가
            else continue//아니면 continue;
        }
        for(int i=0;i<grade.size();i++//i=0부터 grade.size보다 작을때까지 반복, size는 5
        {
            if(grade.get(i)=='A')   //grade의 index에 해당하는 값이 A일때
            {score[i]=4.0;} //score[i]에 4.0 대입
            else if(grade.get(i)=='B'//grade의 index에 해당하는 값이 B일때
            {score[i]=3.0;} //score[i]에 3.0 대입
            else if(grade.get(i)=='C'//grade의 index에 해당하는 값이 C일때
            {score[i]=2.0;} //score[i]에 2.0 대입
            else if(grade.get(i)=='D'//grade의 index에 해당하는 값이 D일때
            {score[i]=1.0;} //score[i]에 1.0 대입
            else if(grade.get(i)=='F'//grade의 index에 해당하는 값이 F일때
            {score[i]=0.0;} //score[i]에 0.0 대입
            System.out.print(score[i]+" "); //socre[i]와 공백문자 출력
            sum+=score[i]; //sum에 sum과 score[i] 더한 값 대입
        }
        System.out.println(); //개행문자 출력
        System.out.print("평균 : "+sum/grade.size() ); //평균 출력}}
 
cs



반응형

'컴퓨터 & 프로그래밍 & 전자공학 > JAVA' 카테고리의 다른 글

제네릭 클래스  (0) 2017.06.19
학생 정보  (0) 2017.06.19
가장 큰 수  (0) 2017.06.19
겜블링 게임  (0) 2017.06.19
가위바위보  (0) 2017.06.19

+ Recent posts