본문 바로가기

프로그램언어/자바

클래스를 배열로 선언해보기

출력 예

###############################################

입력(Y/N) : y

입력1 : a1

입력2 : a2

입력3 : a3

입력4 : a4

입력5 : a5

입력 숫자5 : 100

입력(Y/N) : y

입력1 : s1

입력2 : s2

입력3 : s3

입력4 : s4

입력5 : s5

입력 숫자5 : 200

입력(Y/N) : n

제목1 제목2 제목3 제목4 제목5 제목6

a1 a2 a3 a4 a5 100

s1 s2 s3 s4 s5 200

숫자 합계 : 300

###############################################

 

import java.util.*;

 

class Book{

    public

        String subject1;

        String subject2;

        String subject3;

 

        Book(String a, String b, String c){

            this.subject1= a;

            this.subject2= b;

            this.subject3= c;

        }

}

 

class ListInput extends Book{

    public

        String sub1;

        String sub2;

        int num;

        static int total=0;

 

        ListInput(String a, String b, String c, String d1, String d2, int d3){

            super(a, b, c);

            this.sub1 = d1;

            this.sub2 = d2;

            this.num = d3;

            sum_num();

        }

 

        void sum_num(){

            this.total = this.total +num;

       }

}

 

public class Test {

 

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        String a0, a1, a2, a3, a4;

        String yesno;

        int su, i, j;

 

        ListInput [] test = new ListInput[100];

 

        for(i = 0; ; i++){

            System.out.print("입력(Y/N) : ");

             yesno = scan.nextLine();

             if(yesno.equals("y")){

                 System.out.print("입력1 : ");

                 a0 = scan.nextLine();

                 System.out.print("입력2 : ");

                 a1 = scan.nextLine();

                 System.out.print("입력3 : ");

                 a2 = scan.nextLine();

                 System.out.print("입력4 : ");

                 a3 = scan.nextLine();

                 System.out.print("입력5 : ");

                 a4 = scan.nextLine();

                 System.out.print("입력 숫자5 : ");

                 su = scan.nextInt();

                 test[i] = new ListInput(a0, a1, a2, a3, a4, su);

                 scan.nextLine();

            } else {

                break;

            }

       }

       System.out.println("제목1 제목2 제목3 제목4 제목5 제목6");

       for(j=0; j<i; j++){

           System.out.print(test[j].subject1 + " ");

           System.out.print(test[j].subject2 + " ");

           System.out.print(test[j].subject3 + " ");

           System.out.print(test[j].sub1 + " ");

           System.out.print(test[j].sub2 + " ");

           System.out.println(test[j].num);

        }

        System.out.println("숫자 합계 : " + ListInput.total);

    }

}

 

 

'프로그램언어 > 자바' 카테고리의 다른 글

자바에서 MySql연동을 위한 라이브러리 추가하기  (0) 2022.02.24
학생 관리 프로그램  (0) 2021.08.24
람다식  (0) 2021.06.11
클래스3  (0) 2021.04.12
클래스2  (0) 2021.04.12