본문 바로가기
Java 개발자 수업 강의노트/Java 일일 과제

홀수(양수), 짝수(음수) 출력 프로그램

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

#홀수는 양수로, 짝수는 음수로 출력되게 하는 프로그램

결과물 ex) 1, -2, 3, -4, 5.....

public class AprilEighteen1 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  //1, -2, 3, -4, 5, -6출력
  for(int i=1; i<7; i++){
   if(i%2==0){
    System.out.print((i*-1)+","); // 짝수에 -1을 곱함으로서 음수로 출력
   }else{
    System.out.print(i+",");
   }

  System.out.print(); // 한 줄 내리기

   int direction = 1;
  
   for(int i=1; i<21; i++){
   
   System.out.print(i*direction+"\t");
   direction = direction*-1; // 이 명령어를 출력 명령어 위로 올리면 홀수가 음수로 출력
  }
  }

 }

}

728x90
반응형