본문 바로가기
프로그램/Java 1000제

Java 23제] 1~100까지 중 짝수 출력하시오.(생성자 선언)

by 건티 2022. 12. 15.
728x90

출처 : 반크_세계유산 경복궁

 

문제]

1~100까지의 수 중 짝수를 한줄에 10개씩 출력하는 프로그램을 작성하시오.

 

참고풀이]

public class OnH_Event1 {

   int N; //끝변수

   int i; //반복변수

   int cnt; //조건 수를 카운트하는 변수

   public OnH_Even1(int N)

   {

      this.N=N;

      cnt=0;

   }

 

   public void Show()

   {

      for(i=1;i<=this.N;i++)

         if(i%2==0)

         {

            System.out.printf("%4d",i);

            if(++cnt%10==0) System.out.println();

         }

      System.out.println();

   }

 

   public static void main(String[] args) {

      // TODO Auto-generated mathod stub

      OnH_Event1 onhe = new OnH_Event1(100);

 

      onhe.Show();

   }

}

 

참고풀이 결과]

 

 

 

 

 

대한민국의 아름다운 영토, 독도의 여름

 

반응형

댓글