본문 바로가기

Study/Java

[java] for문의 활용

for문으로 숫자 나열, 별 그리기

test_for1_1.java

class test_for1_1
{
 public static void main(String[] args) 
 {
  int n = 5;
  for(int x=1;x<=(n*n);x++)
  {   
   System.out.print(x+"\t");
   if (x%n == 0)
   {
    System.out.println();
   }
  }
 }
}

 

test_for1_2.java

class test_for1_2
{
 public static void main(String[] args) 
 {
  int n=5;
  for(int x=(n*n);x>0;x--)
  {
   x = x-(n-1);
   for(int y=x; y<(x+n);y++)
   {
    System.out.print(y+"\t");
    if (y%n == 0)
    {
     System.out.println();
    }
   }
  }
 }
}

 

test_for1_3.java

class test_for1_3
{
 public static void main(String[] args) 
 {
  int n = 5;
  for(int x=1;x<=(n*(n*2));x++)
  {   
   System.out.print(x+"\t");
   x=x+1;
   if (x%(n*2) == 0)
   {
    System.out.println();
   }
  }
 }
}

 

test_for1_4.java

class test_for1_4
{
 public static void main(String[] args) 
 {
  int n= 5;
  for(int x=1;x<=n;x++)
  {   
   for (int y=1;y<=x ;y++ )
   {
    System.out.print(y+"\t");
   }
   System.out.println();
  }
 }
}

 

test_for1_5.java

class test_for1_5
{
 public static void main(String[] args) 
 {
  int n = 5;
  int z=0;
  for(int x=1;x<=n;x++)
  {
   for (int y=1;y<=x ;y++ )
   {
    z++;
    System.out.print(z+"\t");
   }
   System.out.println();
  }
 }
}

 

test_for1_6.java

class test_for1_6
{
 public static void main(String[] args) 
 {
  int n = 5;
  for(int x=n;x>=1;x--)
  {   
   for (int y=1;y<=x;y++)
   {
    System.out.print(y+"\t");
   }
   System.out.println();
  }
 }
}

 

test_for1_7.java

class test_for1_7
{
 public static void main(String[] args) 
 {
  int n = 5;
  int z = 1;
  for(int x=n;x>=1;x--)
  {   
   for (int y=1;y<=x;y++)
   {
    System.out.print(z+"\t");
    z++;
   }
   System.out.println();
  }
 }
}

 

test_for1_8.java

class test_for1_8
{
 public static void main(String[] args) 
 {
  int n = 5;
  int z = 0;
  for(int x=1;x<=n;x++)
  {
   for (int y=0;y<=n;y++)
   {    
    System.out.print((x+y)+"\t");
   }
   System.out.println();
  }
 }
}

 

test_for1_9.java

class test_for1_9
{
 public static void main(String[] args) 
 {
  int n = 5;
  int z = 0;
  for(int x=1;x<=n;x++)
  {
   for (int y=0;y<=(n-1);y++)
   {
    z=x+y;
    if (z>n)
    {
     z=z-n;
    }
    System.out.print(z+"\t");
    
   }
   System.out.println();
  }
 }
}

 

test_for1_10.java

class test_for1_10
{
 public static void main(String[] args) 
 {
  int num = 5;
  int z=0;
  for(int x=1;x<6;x++)
  {
   for(int a=5;a>=x;a--)
   {
    System.out.print(" \t");
   }
   for (int y=1;y<=x ;y++ )
   {
    z++;
    System.out.print(z+"\t");
   }
   System.out.println();
  }
 }
}

 

test_for2_1.java

class test_for2_1
{
 public static void main(String[] args) 
 {
  int n = 8;
  for(int x=1;x<=(n*n);x++)
  {   
   System.out.print("*");
   if (x%n == 0)
   {
    System.out.println();
   }
  }
 }
}

 

test_for2_2.java

class test_for2_2
{
 public static void main(String[] args) 
 {
  int n = 5;
  for(int x=1;x<=n;x++)
  {   
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}

 

test_for2_3.java

class test_for2_3
{
 public static void main(String[] args) 
 {
  int n = 5;
  int z=0;
  for(int x=1;x<=n;x++)
  {
   for(int a=n;a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}

 

test_for2_4.java

class test_for2_4
{
 public static void main(String[] args) 
 {
  int n = 5;
  for(int x=1;x<=n;x++)
  {
   for(int a=n;a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int z=2;z<=x;z++)
   {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}

 

test_for2_5.java

class test_for2_5
{
 public static void main(String[] args) 
 {
  int n = 5;
  for(int x=1;x<=n;x++)
  {
   for(int a=n;a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int z=2;z<=x;z++)
   {
    System.out.print("*");
   }
   System.out.println();
  }

  for(int x=1;x<=n;x++)
  {
   for(int a=0;a<=x;a++)
   {
    System.out.print(" ");
   }
   for (int y=(n-1);y>=x ;y--)
   {
    System.out.print("*");
   }
   for(int z=(n-2);z>=x;z--)
   {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}

 

test_for2_6.java

class test_for2_6
{
 public static void main(String[] args) 
 {
  int n = 5;
  for(int x=1;x<=n;x++)
  {
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int z=2;z<=x;z++)
   {
    System.out.print("*");
   }
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int z=2;z<=x;z++)
   {
    System.out.print("*");
   }
   System.out.println();
  }

  for(int x=0;x<n;x++)
  {
   for(int a=0;a<=x;a++)
   {
    System.out.print(" ");
   }
   for (int y=(n-2);y>=x ;y--)
   {
    System.out.print("*");
   }
   for(int z=(n-3);z>=x;z--)
   {
    System.out.print("*");
   }
   for(int a=0;a<=x;a++)
   {
    System.out.print(" ");
   }
   for(int a=0;a<=x;a++)
   {
    System.out.print(" ");
   }
   for (int y=(n-2);y>=x ;y--)
   {
    System.out.print("*");
   }
   for(int z=(n-3);z>=x;z--)
   {
    System.out.print("*");
   }

   System.out.println();
  }
 }
}

 

test_for2_7.java

class test_for2_7
{
 public static void main(String[] args) 
 {
  int n = 5;
  for(int x=1;x<=n;x++)
  {
   //작은삼각형
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int z=2;z<=x;z++)
   {
    System.out.print("*");
   }
   //--작은삼각형
   //n형삼각형
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int z=1;z<=(n*2);z++)
   {   
    System.out.print("*");
   }
   for(int z=1;z<=x;z++)
   {
    System.out.print("*");
   }
   //--n형삼각형
   //2번째 작은삼각형
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int z=2;z<=x;z++)
   {
    System.out.print("*");
   }
   //--2번째 작은삼각형
   System.out.println();
 }
 }
}

 

test_for2_8.java

class test_for2_8
{
 public static void main(String[] args) 
 {
  int n = 5;
  //위 삼각형
  for(int c=1;c<=n;c++)
  {
   for(int a=(n*4)-2;a>=c;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=c ;y++ )
   {
    System.out.print("*");
   }
   for(int z=2;z<=c;z++)
   {
    System.out.print("*");
   }
   System.out.println();
  }
  //--위 삼각형
  for(int x=1;x<=n;x++)
  {
   //작은삼각형
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int z=2;z<=x;z++)
   {
    System.out.print("*");
   }
   //--작은삼각형
   //n형삼각형
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int m=1;m<=((n*2)-1);m++)
   {   
    System.out.print("*");
   }
   for(int z=1;z<=x;z++)
   {
    System.out.print("*");
   }
   //--n형삼각형
   //2번째 작은삼각형
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for(int a=(n-1);a>=x;a--)
   {
    System.out.print(" ");
   }
   for (int y=1;y<=x ;y++ )
   {
    System.out.print("*");
   }
   for(int z=2;z<=x;z++)
   {
    System.out.print("*");
   }
   //--2번째 작은삼각형
   System.out.println();
  }
 }
}

 

test_for2_9.java

class test_for2_9
{
 public static void main(String[] args) 
 {
  int n = 5;
  for(int c=1;c<=(n+2);c++)
  {
   System.out.print("$");
  }
  System.out.println();
  for(int y=1;y<=n;y++)
  {
   System.out.print("$");
   for(int x=1;x<=n;x++)
   {   
    System.out.print("*");
   }
   System.out.println("$");
  }
  
  for(int c=1;c<=(n+2);c++)
  {
   System.out.print("$");
  }
 }
}

 

test_for2_10.java

class test_for2_10
{
 public static void main(String[] args) 
 {
  int n = 10;
  System.out.println("*");
  for(int x=0;x<=n;x++)
  {
   System.out.print("*");
   for(int y=1;y<=x;y++) 
   {   
    System.out.print("@");
   }
   System.out.println("*");
  }
  for(int x=1;x<=n;x++)
  {
   System.out.print("*");
   for(int y=(n-1);y>=x;y--)
   {   
    System.out.print("@");
   }
   System.out.println("*");
  }
  System.out.println("*");
 }
}

'Study > Java' 카테고리의 다른 글

[java] 상속, 인터페이스 실습  (0) 2019.05.15
[java] 상속, 인터페이스  (0) 2019.05.15
[java] 객체지향 실습  (0) 2019.05.14
[java] 객체지향  (0) 2019.05.14
[java] 조건문, 반복문 예제  (0) 2019.05.14