본문 바로가기
Java 개발자 수업 강의노트/Java 소스

홀수 출력 방법

by 캬캬백곰 2012. 4. 18.
728x90

#1~20의 숫자 중 홀수를 출력하는 방법

public class For2 {

 /**
  * @param args
  */
 public static void main(String[] args) {


  // 첫 번째 방법
  for(int i = 1; i<21; i=i+2){
   System.out.print(i+"\t");
  }
  
  System.out.println(); //한 줄 내리기
  

  //두 번째 방법
  for(int i=1; i<21; i++){
   if(i%2==1){
    System.out.print(i+"\t");
   }
  }

 }

}

728x90
반응형