본문 바로가기

자바/문법

for문 안의 for문(별 표 찍기)

예제1)


public class Test1{

public static void main(String[] args) {



int a,b;

for(a=1; a<=5; a++){

System.out.println("a:"+a); // 5번 실행

for(b=1; b<=4; b++){

System.out.print("안쪽a:"+a+",b:"+b); // 20번 실행

}

System.out.println();

System.out.println("-------------------");

}

}

}


예제1 결과)



================================================================


예제2)


public class Test2{

public static void main(String[] args) {



for(int a=2; a<=9; a++){

System.out.println("** " +a +"단 **"); // 8번 실행

for(int b=1; b<=9; b++) {

System.out.printf("%2d*%2d=%2d\n",a,b,(a*b)); // 72번 실행

}

System.out.println();

}

}

}


예제2 결과)


1단에서 9단까지 출력되면 됩니다.


================================================================


예제3)


public class Test3{

public static void main(String[] args) {



int n=0;

for(int a=1; a<=5; a++){  // 재초기화는 돌아왔을 때 실행하므로 처음부터 2가 찍히지 않는다.

for(int b=1; b<=a; b++){

n++;

System.out.println("a:"+a+",b:"+b);

a=10; // 중간에 변수를 넣으면 실행횟수에 영향을 미친다. 지워도 실행가능.

}

}

System.out.println("실행횟수:"+n);

}

}


예제3 결과)


a:1,b:1

a:10,b:2

a:10,b:3

a:10,b:4

a:10,b:5

a:10,b:6

a:10,b:7

a:10,b:8

a:10,b:9

a:10,b:10

실행횟수:10


================================================================


예제4)


public class Test4{

public static void main(String[] args) {



for(int a=1; a<=5; a++){

for(int b=1; b<=5-a; b++) {

System.out.print(" ");

}


for(int b=1; b<=a; b++){

System.out.print("*");

}

System.out.println();

}

}

}


예제4 결과)


    *

   **

  ***

 ****

*****


================================================================


예제5)


public class Test5{

public static void main(String[] args) {



for(int a=1; a<=5; a++){

for(int b=1; b<=5-a; b++) {

System.out.print(" ");

}


for(int b=1; b<=a*2-1; b++){

System.out.print("*");

}

System.out.println();

}

}

}


예제5 결과)


    *

   ***

  *****

 *******

*********  


================================================================


예제6)


public class Test6{

public static void main(String[] args) {



for(int a=1; a<=5; a++){

for(int b=1; b<=5-a; b++) {

System.out.print(" ");

}


for(int b=1; b<=a*2-1; b++){

System.out.print("*");

}

System.out.println();

}


for(int a=4; a>=1; a--){

for(int b=1; b<=5-a; b++) {

System.out.print(" ");

}


for(int b=1; b<=a*2-1; b++){

System.out.print("*");

}

System.out.println();

}

}

}


예제6 결과)


    *

   ***

  *****

 *******

*********  

 *******

  *****

   ***

    *


================================================================


예제7)


// 주석 삭제하건 안하건 결과는 똑같은 모양이다

public class Test7{

public static void main(String[] args) {


for(int a=1; a<=5; a++){

// for(int b=1; b>=2; b++) {

// System.out.print(" ");

// }


for(int b=1; b<=a; b++){

System.out.print("*");

}

System.out.println();

}


for(int a=1; a<=4; a++){

// for(int b=1; b<=4; b++) {

System.out.print("    ");

// }


for(int b=1; b<=a+1; b++){

System.out.print("*");


}

System.out.println();

}

}

}


예제7 결과)


*

**

***

****

*****

      **

      ***

      ****

      *****


================================================================


예제8)


// 주석 삭제하건 안하건 결과는 똑같은 모양이다
public class Test8{
public static void main(String[] args) {

/*        다이아 모양의 별모양이 나온다.
for(int a=5; a>=1; a--){
for(int b=1; b<=5-a; b++)  // 여기서의 변수b는 for안에서만 적용되므로 바로 아래 for문의 변수b와 중복되지 않는다.
System.out.print(" ");
for(int b=1; b<=a*2-1; b++)
System.out.print("*");
System.out.println();
}


for(int a=2; a<=5; a++){
for(int b=1; b<=5-a; b++) 
System.out.print(" ");
for(int b=1; b<=a*2-1; b++)
System.out.print("*");
System.out.println();
}
*/

for(int a=1; a<=5; a++) {
for(int b=1; b<=5-a; b++)
System.out.print(" ");
for(int b=1; b<=a*2-1; b++){
if(b==1 ||b==a*2-1)
System.out.print("*");
else
System.out.print(" ");

}
System.out.println();
}

for(int a=4; a>=1; a--) {
for(int b=1; b<=5-a; b++)
System.out.print(" ");
for(int b=1; b<=a*2-1; b++){
if(b==1 ||b==a*2-1)
System.out.print("*");
else
System.out.print(" ");

}
System.out.println();
}

}
}

예제8 결과)

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

================================================================


예제9)


public class Test9{

public static void main(String[] args) {



for(int a=1; a<=5; a++){

for(int b=1; b<=5-a; b++) {

System.out.print(" ");

}


for(int b=1; b<=a*2-1; b++){

System.out.print("*");

}

System.out.println();

}


for(int a=4; a>=1; a--){

for(int b=1; b<=5-a; b++) {

System.out.print(" ");

}


for(int b=1; b<=a*2-1; b++){

System.out.print("*");

}

System.out.println();

}

}

}


예제9 결과)


    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

    *


'자바 > 문법' 카테고리의 다른 글

switch ~ case문  (0) 2013.05.09
do~ while과 for문을 이용한 프로그램  (0) 2013.05.09
for 문  (0) 2013.05.09
do~while 문법  (0) 2013.05.09
while 문법  (0) 2013.05.09