JAVA chapter 04. 조건문과 반복문. 


4.1 코드 실행 흐름제어.



4.2 조건문 (if문, switch문)

4.2.1 if문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package sec02.exam01_if;
 
public class IfExample {
    public static void main(String[] args) {
        int score = 93;
        
        if(score>=90) {
            System.out.println("점수가 90보다 큽니다.");
            System.out.println("등급은 A 입니다.");
        }
        
        if(score< 90) // 중괄호 {} 블록을 생략하지 않고 작성하기 - 코드의 가독성이 좋지 않고, 버그 발생의 원인이 됨
            System.out.println("점수가 90보다 작습니다.");
            System.out.println("등급은 B 입니다.");
    }
}
cs



4.2.2 if-else문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package sec02.exam01_if;
 
public class IfElseExample {
    public static void main(String[] args) {
        int score = 85;
        
        if(score>=90) {
            System.out.println("점수가 90보다 큽니다.");
            System.out.println("등급은 A 입니다.");
        } else {    
            System.out.println("점수가 90보다 작습니다.");
            System.out.println("등급은 B 입니다.");
        }
    }
}
cs



4.2.3 if-else if-else문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package sec02.exam01_if;
 
public class IfElseIfElseExample {
    public static void main(String[] args) {
        int score = 75;
        
        if(score>=90) {
            System.out.println("점수가 100~90 입니다.");
            System.out.println("등급은 A 입니다.");
        } else if(score>=80) {    
            System.out.println("점수가 80~89 입니다.");
            System.out.println("등급은 B 입니다.");
        } else if(score>=70) {
            System.out.println("점수가 70~79 입니다.");
            System.out.println("등급은 C 입니다.");
        } else {
            System.out.println("점수가 70 미만 입니다.");
            System.out.println("등급은 D 입니다.");
        }
    }
}
cs


// int num = (int)(Math.random() * n) + start
주사위의 번호를 뽑는 예제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package sec02.exam01_if;
public class IfDiceExample {
    public static void main(String[] args) {
        int num = (int)(Math.random()*6+ 1;
        
        if(num==1) {
            System.out.println("1번이 나왔습니다.");
        } else if(num==2) {    
            System.out.println("2번이 나왔습니다.");
        } else if(num==3) {
            System.out.println("3번이 나왔습니다.");
        } else if(num==4) {
            System.out.println("4번이 나왔습니다.");
        } else if(num==5) {
            System.out.println("5번이 나왔습니다.");
        } else {
            System.out.println("6번이 나왔습니다.");
        }
    }
}
cs




4.2.4 중첩 if문

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
package sec02.exam01_if;
public class IfNestedExample {
    public static void main(String[] args) {
        int score = (int)(Math.random()*20+ 81;
        System.out.println("점수: " + score);
        
        String grade;
        
        if(score>=90) {
            if(score>=95) {
                grade = "A+";
            } else {
                grade = "A";
            }
        } else {    
            if(score>=85) {
                grade = "B+";
            } else {
                grade = "B";
            }
        }
        
        System.out.println("학점: " + grade);
    }
}
cs


4.2.5 switch문

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
package sec02.exam02_switch;
 
public class SwitchExample {
    public static void main(String[] args) {
        int num = (int)(Math.random()*6+ 1;
        
        switch(num) {
            case 1:
                System.out.println("1번이 나왔습니다.");
                break;
            case 2:
                System.out.println("2번이 나왔습니다.");
                break;
            case 3:
                System.out.println("3번이 나왔습니다.");
                break;
            case 4:
                System.out.println("4번이 나왔습니다.");
                break;
            case 5:
                System.out.println("5번이 나왔습니다.");
                break;
            default:
                System.out.println("6번이 나왔습니다.");
                break;
        }
    }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package sec02.exam02_switch;
 
public class SwitchNoBreakCaseExample {
    public static void main(String[] args) {
        //8<= … < 12(8+4) 사이의 정수 얻기
        int time = (int)(Math.random()*4+ 8
        System.out.println("[현재시간: " + time + " 시]");
        
        switch(time) {
            case 8:
                System.out.println("출근합니다.");
            case 9:
                System.out.println("회의를 합니다.");
            case 10:
                System.out.println("업무를 봅니다.");
            default:
                System.out.println("외근을 나갑니다.");
        }
    }
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package sec02.exam02_switch;
 
public class SwitchCharExample {
    public static void main(String[] args) {
        char grade = 'B';
        
        switch(grade) {
            case 'A':
            case 'a':
                System.out.println("우수 회원입니다.");
                break;
            case 'B':
            case 'b':
                System.out.println("일반 회원입니다.");
                break;                            
            default:
                System.out.println("손님입니다.");
        }
    }
}

cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package sec02.exam02_switch;
 
public class SwitchStringExample {
    public static void main(String[] args) {
        String position = "과장";
        
        switch(position) {
            case "부장":
                System.out.println("700만원");
                break;
            case "과장":
                System.out.println("500만원");
                break;                            
            default:
                System.out.println("300만원");
        }
    }
}
cs




Posted by 너래쟁이
: