/* 문제: 1 [홀수 짝수]
 * 0 ~ 9까지 숫자를 출력하고 각 숫자가 홀수인지 짝수인지 출력한다.
 * tip:  switch, if, ? :
 * 출력 예:
 * 0(짝수)
 * 1(홀수)

 

//  삼항연산자 사용

 

class Test{

  public static void main(String[] args) throws Exception {
    for(int i = 0; i <= 10; i++) {
      System.out.printf("%d : (%s) \n", i, (i % 2) == 0 ? "짝수" : "홀수") ;
    }
   }
}

 

//  if 조건문

class Test{

  public static void main(String[] args) throws Exception {
    for(int i = 0; i <= 10; i++) {
      if (i % 2 == 0) {
        Print(i, "짝수");
      } else if (i % 2 != 0) {
        Print(i, "홀수");
      } else {
        System.out.printf("응?");
      }
    }
   }
   static void Print(int no, String odd_even ) {
     System.out.printf("%d (%s) \n", no, odd_even);
   }
}

 

// switch case

class Test{

  public static void main(String[] args) throws Exception {
    for(int i = 0; i <= 10; i++) {

      switch (i % 2) {
        case 0 : Print(i, "짝수");
        break;
        case 1 : Print(i, "홀수");
        break;
        default :
        break;
      }
    }
   }
   static void Print(int no, String odd_even) {
     System.out.printf("%d (%s) \n", no, odd_even);
   }
}

Posted by 안낭우훗

블로그 이미지
좋은싸이트 공유, 재해석 , 공부 정리, 틀린거 알려 주세요~
안낭우훗

태그목록

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.6
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

최근에 올라온 글

최근에 달린 댓글

글 보관함