ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [7] for - multi
    Develpment/Java Sample Source 2020. 9. 13. 21:27

    for 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package control_statement02;
     
    public class Multi_For01 
    {
        public static void main(String[] args) 
        {
            // 다중 for 문 : for문 안에 for문이 있는 경우
            
            for(int i = 1; i <= 3; i++)
            {
                for(int j = 1; j <= 5; j++)
                {
                    if(j%2 != 0)
                        System.out.print(j + " ");
                }
                System.out.println();
            }
        }
    }
    cs


    실행결과 

    1 3 5 

    1 3 5 

    1 3 5 


    for 2 

    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 control_statement02;
     
    public class Multi_For02 
    {
        public static void main(String[] args) 
        {
            //int nNum = 0;
            
            for(int i = 0; i <= 1 ; i++)
            {
                for(int j = 1; j <= 4; j++)
                {
                    //nNum++;
                    //System.out.print(nNum + " ");
                    
                    System.out.print((j + i*4+ " ");
                    
                }
                System.out.println();
            }
            
            System.out.println("---------------------------------------------------");
            
            
            
            
            for(int i = 0; i <= 1 ; i++)
            {
                for(int j = 1; j <= 4; j++)
                {
                    char cTemp = 'A';
                    cTemp += (j + i*4- 1;
                    System.out.print( cTemp + " ");
                }
                System.out.println();
            }
            
        }
    }
    cs


    실행결과 

    1 2 3 4 

    5 6 7 8 

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

    A B C D 

    E F G H 


    for 3 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package control_statement02;
     
    public class Multi_For03 
    {
        public static void main(String[] args) 
        {
            for(int i = 1; i <= 5; i++)
            {
                for(int j = 1; j <= i; j++)
                {
                    System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
    cs


     실행결과

    * * 

    * * * 

    * * * * 

    * * * * * 


    for 4 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package control_statement02;
     
    public class Multi_For04 
    {
        public static void main(String[] args) 
        {
            for(int i = 0; i < 10; i++)
            {
                for(int j = i; j < i+10; j++)
                {
                    System.out.printf("%02d ", (j%10)+1);
                }
                System.out.println();
            }
     
        }
    }
    cs


    실행결과 

    01 02 03 04 05 06 07 08 09 10 

    02 03 04 05 06 07 08 09 10 01 

    03 04 05 06 07 08 09 10 01 02 

    04 05 06 07 08 09 10 01 02 03 

    05 06 07 08 09 10 01 02 03 04 

    06 07 08 09 10 01 02 03 04 05 

    07 08 09 10 01 02 03 04 05 06 

    08 09 10 01 02 03 04 05 06 07 

    09 10 01 02 03 04 05 06 07 08 

    10 01 02 03 04 05 06 07 08 09 


    for 5 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package control_statement02;
     
    public class Multi_For05 
    {
        public static void main(String[] args) 
        {
            for(int i = 4; i >= 0; i--)
            {
                for(int j = 0; j < i+5; j++)
                {
                    if(i+j> 3)
                        System.out.print("*");
                    else
                        System.out.print(" ");
                }
                System.out.println();
            }
        }
    }
    cs


    실행결과 

    *********

     *******

      *****

       ***

        *


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

    [9] do-while  (0) 2020.09.13
    [8] while  (0) 2020.09.13
    [6] for - single  (0) 2020.09.13
    [5] switch  (0) 2020.09.13
    [4] if  (0) 2020.09.13

    댓글

Designed by Tistory.