..열심히 공부하세../JAVA 문법

[08] 이중 for문 예제

댄스댄스 2012. 4. 19. 13:49

이중 for문

for( 초기식; 조건식; 증감식 ) {

    for( 초기식; 조건식; 증감식 ) {

       // 수행문;

    }

}

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

 

파일명:TestFor2.java

public class TestFor2{
  public static void main(String[] args){
/*
for(초기값; 종료값 또는 조건식; 증감)
{
     for(초기값; 종료값 또는 조건식; 증감)
  {

     }
}
*/
/*
     int p,q;
     for(p=2; p<=9; p++){
          System.out.println(p+" 단");

    for(q=1; q<=9; q++){
             System.out.println(p + "*" + q + "=" + (p*q));
    }
  }
*/
/*
      int p,q,hap=0;
      for(p=10; p<=100; p+=10){
         
    for(q=1; q<=p; q++){
     hap=hap+q;
          }

    System.out.println("1+...+" + p + "=" + hap);
    hap=0;
   }
*/
/*
1+...+10=55
1+...+20=
1+...+30=
1+...+40=
1+...+50=
1+...+60=
1+...+70=
1+...+80=
1+...+90=
1+...+100=5050
*/
/*
         int p, q, hap=0;
   for(p=10; p<=100; p=p+10){
 
              for(q=p-9; q<=p; q++){
                  hap=hap+q;
     }

     System.out.println( (p-9) + "+...+" + p + "=" + hap);
     hap=0;
   }
*/
/*
1+...+10=55
11+...+20=
21+...+30=
31+...+40=
41+...+50=
51+...+60=
61+...+70=
71+...+80=
81+...+90=
91+...+100=
*/

            double days=365.2422;
   long hap=(int)(days*86400); //1년이 몇초?

   int day=(int)(hap/86400); // 365 일
   hap=hap%86400;
   int hour=(int)(hap/3600); // ?시간
   hap=hap%3600;
   int minute=(int)(hap/60); // ?분
   int second=(int)(hap%60); // ?초

   System.out.println(days+"= ");
   System.out.println(day+"일 ");
   System.out.println(hour+"시간 ");
   System.out.println(minute+"분 ");
   System.out.println(second+"초 ");

/*
365.2422일 -> 365일 ?시간 ?분 ?초
1분 60초
1시간(60분) 3600초=(60분*60초)
24시간(1일) 86400초=(24시간*3600초)
1년이 몇초? 86400초*365.2422일=31556926.08

(31556926.08) / 86400 -> 365 (몫)
(86400*365.2422) % 86400 ->
*/

/*
윤년구하는 공식
4년마다 366일 -> 2월 29일
*/

        int y=2012;
  if(y%4==0 && y%100!=0 || y%400==0)
   //4년마다 윤년이고 100년에 한번씩은 윤년처리를 안함
      //400년마다 윤년
   System.out.println("윤년");
        else
   System.out.println("평년");


  }//end
}//end