본문 바로가기

콩's EDUCATION/콩's JAVA_RUN

구구단 반복문

구구단과 '*'을 이용한 도형 만들기는 반복문의 꽃입니다.


 

public class GugudanTest {
 public static void main(String[] args) {
   //다중 for문
  System.out.println("=======================");
  System.out.println("다중 for 문");
  System.out.println("=======================");
  for(int i=2;i<10;i++){
   System.out.print(i+"단 : ");
   for(int j=1;j<10;j++){
    int sum=i*j;      
    System.out.print(i+"*"+j+"="+sum+" ");

    //i값 1, j값은 2로 변경후 System.out.print(j+"*"+i+"="+sum+" ");
    }
   System.out.println();
  }
   //do~while문, 2단.
  System.out.println("=======================");
  System.out.println("do~while문, 2단");
  System.out.println("=======================");

  int j = 1;
  do{
   System.out.println(2+"*"+j+"="+2*j);
   j++;
  }while(j<=9);
   //while 문, 2단
  System.out.println("=======================");
  System.out.println("while 문, 2단");
  System.out.println("=======================");
  int j2 = 1;
  while(j2<=9){
  System.out.println(2+"*"+j2+"="+2*j2);
  j2++;
  }
 }
}

 

 

GugudanTest.java