ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [14] arrary-multi
    Develpment/Java Sample Source 2020. 9. 13. 21:29

     array 

    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
    package multi_array;
     
    public class Multi_Array01 
    {
        public static void main(String[] args) 
        {
            // 다차원 배열
            // 1차원 배열이 2개 모이면 2차원 배열
            // 2차원 배열이 3개 모이면 3차원 배열
            
            int[][] arr = new int[2][3];
            
            arr[0][0= 10;
            arr[0][1= 20;
            arr[0][2= 30;
            
            arr[1][0= 40;
            arr[1][1= 50;
            arr[1][2= 60;
            
            //System.out.println(arr[1][1]);
            
            for(int i = 0; i < arr.length; i++)
            {
                for(int j = 0; j < arr[i].length; j++)
                    System.out.printf("%d " , arr[i][j]);
                System.out.println();
            }
            
            for(int i = 0; i < arr.length; i++)
            {
                for(int n : arr[i])
                {
                    System.out.printf("%d " , n);
                }
                System.out.println();
            }
        }
    }
    cs


    실행결과 

    10 20 30 

    40 50 60 

    10 20 30 

    40 50 60 


     array 2 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package multi_array;
     
    public class Multi_Array02 
    {
        public static void main(String[] args) 
        {
            String[][] java = { {"영희""j:100""a:80"},
                                {"철수""j:60""a:70""b:90"}    };
            
            for(int i = 0; i < java.length; i++)
            {
                for(String str : java[i])
                    System.out.printf("%s\t ",str);
                System.out.println();
            }
        }
    }
    cs


     실행결과

    영희 j:100 a:80  

    철수 j:60 a:70 b:90


     array 3 

    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
    package multi_array;
     
    import org.omg.Messaging.SyncScopeHelper;
     
    public class Multi_Array03 
    {
        public static void main(String[] args) 
        {
            int n = 0;
            int[][] num = new int[2][];
            
            num[0= new int[3];
            num[1= new int[2];
            
            
            
            for(int i = 0; i < num.length; i++)
            {
                for(int j = 0; j < num[i].length; j++)
                {
                    num[i][j] = n += 10;
                    System.out.printf( "%d ", num[i][j]);
                }
                System.out.println();
            }
        }
    }
    cs


     실행결과

    10 20 30 

    40 50 


     array 4

    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
    package multi_array;
     
    public class Multi_Array04 
    {
        public static void main(String[] args) 
        {
            double dResult = 0;
            int nCount = 0;
            
            int arr[][] = { { 1,  2,  3,  4,  5},
                            { 6,  7,  8,  910},
                            {1112131415},
                            {16171819} };
            
            for(int i = 0; i < arr.length; i++)
                for(int n : arr[i])
                {
                    dResult += n;
                    nCount++;
                }
            
            System.out.println(dResult/nCount);
                    
        }
    }
    cs


     실행결과

    10.0


     array 5 

    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
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    package multi_array;
     
    import java.util.Scanner;
     
    public class Multi_Array05 
    {
        public static void main(String[] args)
        {
            // 수학, 영어 성적 등록
            // 몇명의 정보 인지 입력
            // 입력받은 수 만큼 이름, 수학, 영어 성적 입력받음.
            
            /* 등록할 인원 : 2
             * 이름 : 홍길동
             * 수학 : 90
             * 영어 : 80
             * ------------------
             * ...
             * ------------------
             * 2명 등록 완료!!!
             * 홍길동    90    80
             * ...
             */
            
            String[][] strStudent;
            String[] strMsg = {"이름 : ""수학 : ""영어 : "};
            int nNumber = 0;
            
            Scanner sc = new Scanner(System.in);
            
            System.out.print("등록할 인원 : ");
            nNumber = sc.nextInt();
            
            strStudent = new String[nNumber][strMsg.length];
            
            for(int i = 0; i < strStudent.length; i++)
            {
                for(int j = 0; j < strStudent[i].length; j++)
                {
                    System.out.print(strMsg[j]);
                    strStudent[i][j] = sc.next();
                }
                System.out.println("-------------------------");
            }
            
            
            System.out.printf("%d명 등록완료!!!\n", strStudent.length);
            
            for(int i = 0; i < strStudent.length; i++)
            {
                for(String str : strStudent[i])
                    System.out.printf("%s\t", str);
                System.out.println();
            }
        }
    }
    cs


    실행결과 

     등록할 인원 : 2

    이름 : a

    수학 : 10

    영어 : 20

    -------------------------

    이름 : b

    수학 : 20

    영어 : 30

    -------------------------

    2명 등록완료!!!

    a 10 20

    b 20 30


     array 6 

    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
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    package multi_array;
     
    import java.util.Scanner;
     
    public class Multi_Array06
    {
        public static void main(String[] args)
        {
            // 위 오른쪽 + 1
            // n 의 배수는 아래 +1
            
            // 1은 맨위줄 가운데
            
            int[][] arr;    // 마방진
            int num = 1;
            
            int size = 0;    // 마방진 사이즈
            int y = 0;        // 행
            int x = 0;        // 열
            
            Scanner sc = new Scanner(System.in);
            
            System.out.print("홀수를 입력하세요 : ");
            size = sc.nextInt();
            
            x = size / 2;    // 맨위줄 중앙
            
            arr = new int[size][size];
            
            while(num <= size * size)
            {
                arr[y][x] = num;
                
                if(num % size == 0)
                    y++;
                else
                {
                    y--;    // 위 +1
                    x++;    // 오른쪽 +1
                }
                
                if(y < 0 )
                    y = size -1;
                
                if(x >= size)
                    x = 0;
                
                num ++;
            }
            
            for(int i = 0; i < arr.length ; i++)
            {
                for(int j = 0; j < arr[i].length; j++)
                    System.out.printf("%02d\t", arr[i][j]);
                System.out.println();
            }
        }
    }
    cs


    실행결과 

    홀수를 입력하세요 : 7

    30 39 48 01 10 19 28

    38 47 07 09 18 27 29

    46 06 08 17 26 35 37

    05 14 16 25 34 36 45

    13 15 24 33 42 44 04

    21 23 32 41 43 03 12

    22 31 40 49 02 11 20


    'Develpment > Java Sample Source' 카테고리의 다른 글

    [16] Class  (0) 2020.09.13
    [15] String  (0) 2020.09.13
    [13] Array-single  (0) 2020.09.13
    [12] random, scanner  (0) 2020.09.13
    [11] continue, label  (0) 2020.09.13

    댓글

Designed by Tistory.