자바

이중 for문 예제

chantleman 2024. 7. 8. 11:47

1   2    3    4

5   6    7    8

9  10  11  12

13 14 15  16

 

for(int i=0;i<4;i++)
{
	for(int j=0;j<4;j++)
	{
		int result=i*4+j+1;
		System.out.print(result+"\t");
	}
	System.out.println();
	}
}

 


1   2    3    4

8   7    6    5

9  10  11  12

16 15 14  13

for(int i=0;i<4;i++)
{
	if((i+1)%2==1)
	{
		for(int j=0;j<4;j++)
		{
			int result = i*4+j+1;
			System.out.print(result+"\t");
		}		
	}
	else {	
		for(int j=3;j>=0;j--)
		{
			int result= i*4+j+1;					
			System.out.print(result+"\t");
		}				
	}
		System.out.println();
			
	}
  }
for(int i=0;i<4;i++)
{
	for(int j=0;j<4;j++)
	{
		int result=0;
		if(i%2==0) result= i*4+j+1;
		if(i%2==1) result = i*4+4-j;
		System.out.print(result +"\t");
	}
	System.out.println();
}

 


1  5  9  13

2  6  10 14

3  7  11  15

4  8  12  16

for(int i=0;i<4;i++)
{
	for(int j=0;j<4;j++)
	{
		int result=i+j*4+1;
		System.out.print(result+"\t");
	}
	System.out.println();	
}

 

1  8  9  16
2  7  10 17
3  6  11 14
4  5  12 13

 

for(int i=0;i<4;i++)
{
	for(int j=0;j<4;j++)
	{
		int result=0;
		if(j%2==0) result=i+j*4+1;
		else result=-i+j*4+4;
		System.out.print(result+"\t");
	}
	System.out.println();
}

 

*
**
***
****

System.out.print("row입력: ");
int row=sc.nextInt();
for(int i=0;i<row;i++)
{
	for(int j=0;j<i+1;j++)
	{
		System.out.print("*");
	}
	System.out.println();
}

 

   *

  **
 ***
****

System.out.print("row입력: ");
		int row=sc.nextInt();
		
for(int i=0;i<row;i++)
{
	for(int space=0;space<row-1-i;space++)
	{
		System.out.print(" ");
	}
	for(int star=0;star<i+1;star++)
	{
		System.out.print("*");
	}
	System.out.println();
}

 

 

****   
*** 
**
*

System.out.print("row입력: ");
int row=sc.nextInt();
for(int i=0;i<row;i++)
{
	for(int j=0;j<=row-i-1;j++)
	{
		System.out.print("*");
	}
	System.out.println();
}

 

****   
 *** 
  **
   *

System.out.print("row입력: ");
int row=sc.nextInt();
for(int i=0;i<row;i++)
{
	for(int space=0;space<i;space++)
	{
		System.out.print(" ");
	}
	for(int star=0;star<row-i;star++)
	{
		System.out.print("*");
	}
	System.out.println();
}

 

    *   
   *** 
  *****
 *******

System.out.print("row입력: ");
int row=sc.nextInt();
		
for(int i=0;i<row;i++)
{
	for(int space=row-i;space>=0;space--)
	{
		System.out.print(" ");
	}
	for(int star=0;star<i*2+1;star++)
	{	
		System.out.print("*");
	}
	System.out.println();
}

 

*******    
 ***** 
  ***
   *

System.out.print("row입력: ");
int row=sc.nextInt();		
for(int i=0;i<row;i++)
{
	for(int space=0;space<i;space++)
	{
		System.out.print(" ");
	}
	for(int star=0;star<(row-i-1)*2+1;star++)
	{
		System.out.print("*");
	}
	System.out.println();
}

 


 

*   * 
 * *
  *
 * *

*   *

int n=5;
for(int i=0;i<n;i++)
{
	for(int j=0;j<n;j++)
	{
		if(j==i || j==n-i-1)
		{
			System.out.print("*");
		}
		else
		{
			System.out.print(" ");
		}
	}
			
	System.out.println();
}

'자바' 카테고리의 다른 글

스택, 힙, equals(), ==  (0) 2024.07.10
for문 break, continue  (0) 2024.07.09
시와 분을 입력받고 30분 뒤, 30분 전 시간 출력하는 예제  (0) 2024.07.05
대문자 소문자로 바꾸기 예제  (1) 2024.07.03
String 타입 변환  (0) 2024.07.03