컴퓨터프로그래밍및실습 (2022년)/조건문

From DISLab
Jump to navigation Jump to search

if 문

public class IfExample {
    public static void main(String[] args) {
        int score = 93;

        //-----------------------------------------
        // if 문
        //-----------------------------------------

        if (score >= 90) {
            System.out.println("A");
        }

        if (score < 90) {
            System.out.println("B");
        }

        //-----------------------------------------
        // if-else 문
        //-----------------------------------------

        if (score >= 90) {
            System.out.println("A");
        } else {
            System.out.println("B");
        }

        //-----------------------------------------
        // if-else if-else 문
        //-----------------------------------------

        if (score >= 90) {
            System.out.println("A");
        } else if (score >= 80) {
            System.out.println("B");
        } else if (score >= 70) {
            System.out.println("C");
        } else if (score >= 60) {
            System.out.println("D");
        } else {
            System.out.println("F");
        }

        //-----------------------------------------
        // 중첩 if 문
        //-----------------------------------------

        if (score >= 90) {
            if (score >= 95) {
                System.out.println("A+");
            } else {
                System.out.println("A0");
            }
        } else {
            if (score >= 85) {
                System.out.println("B+");
            } else {
                System.out.println("B0");
            }
        }
    }
}


switch 문

public class SwitchExample {
    public static void main(String[] args) {
        int num = (int) (Math.random() * 10); // 0 ~ 9

        switch(num) {
            case 1 :
                System.out.println("1");
                break;
            case 2 :
                System.out.println("2");
                break;
            case 3 :
                System.out.println("3");
            case 4 :
                System.out.println("4");
                break;
            case 5 :
            case 6 :
            case 7 :
                System.out.println("789");
                break;
            default :
                System.out.println("OOPS");
                break; // break가 필요한가?
        }

        //----------------------------
        //  문자
        //----------------------------

        char grade = (char)('A' + num);

        switch(grade) {
            case 'A' :
                System.out.println("A");
                break;
            case 'B' :
                System.out.println("B");
                break;
            case 'C' :
                System.out.println("C");
                break;
            case 'D' :
                System.out.println("D");
                break;
            default:
                System.out.println("F");
                break;
        }

        //----------------------------
        //  String (JDK 7부터)
        //----------------------------

        String str = "과장";

        switch(str) {
            case "부장" :
                System.out.println("700만원");
                break;
            case "과장" :
                System.out.println("500만원");
                break;
            default:
                System.out.println("300만원");
        }
    }
}