ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

์ œ์–ด๋ฌธ


- ์ œ์–ด๋ฌธ์˜ ์ข…๋ฅ˜์—๋Š” ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ, ํƒˆ์ถœ๋ฌธ์ด ์žˆ๋‹ค.

- ์กฐ๊ฑด๋ฌธ์€ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ ์‹คํ–‰ํ•  ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•˜๋Š”๋ฐ ์‚ฌ์šฉ

- ๋ฐ˜๋ณต๋ฌธ์€ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณต ์‹คํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š”๋ฐ ์‚ฌ์šฉ

- ๋ฐ˜๋ณต๋ฌธ์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์‹คํ–‰์„ ์ค‘์ง€์‹œํ‚ค๊ณ  ์‹ถ์œผ๋ฉด ํƒˆ์ถœ๋ฌธ์„ ์‚ฌ์šฉ

 

 

 

 

 

์กฐ๊ฑด๋ฌธ if ~ else


- if๋ฌธ์€ ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„์„ ๋ฐ”๊พธ๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์กฐ๊ฑด ์„ ํƒ ๋ถ„๊ธฐ๋ฌธ์ด๋‹ค.

- if๋ฌธ์˜ ์กฐ๊ฑด์‹ ๊ฒฐ๊ณผ๋Š” ๋ฐ˜๋“œ์‹œ ๋ถ€์šธํ˜•์ด์–ด์•ผํ•˜๋ฉฐ, ์กฐ๊ฑด์‹์ด ์ฐธ์ด๋ฉด if๋ฌธ ๋ธ”๋ก ์•ˆ์˜ ์‹คํ–‰๋ฌธ์ด ์‹คํ–‰๋˜๊ณ , ๊ฑฐ์ง“์ด๋ฉด else๋ฌธ ๋ธ”๋ก์•ˆ์˜ ์‹คํ–‰๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.

- if๋ฌธ์žฅ์€ else๋ฌธ์žฅ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ else๋ฌธ์žฅ์€ if๋ฌธ์žฅ ์—†์ด ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

- ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฑฐ์ง“์ผ ๋•Œ ์‹คํ–‰ํ•  ๋ฌธ์žฅ์ด ์—†๋‹ค๋ฉด, else๋ฌธ ์ดํ•˜๋ฅผ ์ƒ๋žตํ•ด๋„ ๋œ๋‹ค.

 

 

 

 

๋‹ค์ค‘ ๋ถ„๊ธฐ ์กฐ๊ฑด๋ฌธ if ~ else if ~ else


- ์—ฌ๋Ÿฌ ์กฐ๊ฑด๋“ค์„ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์กฐ๊ฑด๋ฌธ์ด๋‹ค.

- ๋‹ค์ค‘ ๋ถ„๊ธฐ ์กฐ๊ฑด๋ฌธ์€ ์œ„์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์กฐ๊ฑด์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ifEx1 {
 
    public static void main(String[] args) {
        
        //0~100๊นŒ์ง€์˜ ์ •์ˆ˜ํ˜• ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ
        int num = (int)(Math.random() * 101);
        System.out.println("์ •์ˆ˜ : " + num);
        
        if(num >= 60) {
            System.out.println("60์  ์ด์ƒ์ž…๋‹ˆ๋‹ค.");
        } else {
            System.out.println("60์  ๋ฏธ๋งŒ์ž…๋‹ˆ๋‹ค.");
        }
        
        System.out.println("์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.");
    }
}
 
 
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
31
import java.util.Scanner;
 
public class ifEx2 {
    
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        System.out.print("์ ์ˆ˜๋ฅผ ์ž…๋ ฅ > ");
        int score = scan.nextInt();
        
        if(score >= 90) {
            if(score > 100) {
                System.out.println("์ž˜๋ชป๋œ ์ ์ˆ˜ ์ž…๋‹ˆ๋‹ค.");
            } else if(score >=95) {
                System.out.println("A+ํ•™์  ์ž…๋‹ˆ๋‹ค.");
            } else {
                System.out.println("Aํ•™์  ์ž…๋‹ˆ๋‹ค.");
            }
        } else if(score >= 80) { //80~89
            System.out.println("Bํ•™์  ์ž…๋‹ˆ๋‹ค.");
        } else if(score >= 70) { //70~79
            System.out.println("Cํ•™์  ์ž…๋‹ˆ๋‹ค.");            
        } else if(score >= 60) {
            System.out.println("Dํ•™์  ์ž…๋‹ˆ๋‹ค.");
        } else {
            System.out.println("Fํ•™์  ์ž…๋‹ˆ๋‹ค.");
        }
        
    }
}
 
 

 

์กฐ๊ฑด๋ฌธ switch ~ case


- ๋ณตํ•ฉ if๋ฌธ์€ ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณต์žกํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์˜ ํšจ์œจ์„ฑ์ด ๊ฐ์†Œ๋˜๋Š” ๋‹จ์ ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์ค‘ ๋ถ„๊ธฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ๋•Œ๋Š” switch๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.

- switch๋ฌธ์€ ๋‹ค์ค‘ ๋ถ„๊ธฐ ๊ตฌ์กฐ์ด๋ฏ€๋กœ ๋ณตํ•ฉ if๋ฌธ์˜ ๋…ผ๋ฆฌ์ ์ธ ๊ตฌ์กฐ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

- if๋ฌธ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์กฐ๊ฑด์‹์ด ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ , ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ณ€์ˆ˜ ๋˜๋Š” ํ‘œํ˜„์‹์ด ํŒ๋‹จ์กฐ๊ฑด์œผ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ int, String, Enum(์—ด๊ฑฐํ˜•)์ด ์‚ฌ์šฉ๋œ๋‹ค.

- case๋ฌธ ๋’ค์— ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’์€ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ , ๋ฐ˜๋“œ์‹œ ์ƒ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

- switch ~ case๋ฌธ์—์„œ default๋Š” if~else์—์„œ else์™€ ๋น„์Šทํ•œ ํšจ๊ณผ๋ฅผ ๊ฐ€์ง„๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
public class SwitchEx1 {
 
    public static void main(String[] args) {
        
        int num = 10;
        
        switch(num) {
        
        case 1:
            System.out.println("1์ž…๋‹ˆ๋‹ค");
            break;
        case 2:
            System.out.println("2์ž…๋‹ˆ๋‹ค");
            break;
        case 3:
            System.out.println("3์ž…๋‹ˆ๋‹ค");
            break;
        default :
            System.out.println("1~3์ด ์•„๋‹™๋‹ˆ๋‹ค");
        }    
    }
}
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.Scanner;
 
public class SwitchEx2 {
 
    public static void main(String[] args) {
 
        Scanner scan = new Scanner(System.in);
        System.out.print("์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>");
        
        int point = scan.nextInt();
        /*
         * switchํ‚ค์›Œ๋“œ ๋’ค์— ๋‚˜์˜ค๋Š” ์‹์€
         * ๋ณ€์ˆ˜๋‚˜, ๋ณ€์ˆ˜์˜ ์—ฐ์‚ฐ์‹์„ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค
         * 
         * ๋ณ€์ˆ˜์˜ ๊ฐ’์ด๋‚˜, ์—ฐ์‚ฐ์‹์˜ ๊ฒฐ๊ณผ๋Š” ์ •์ˆ˜๋‚˜ ๋ฌธ์žํ˜• ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค
         */
        
        switch(point/10) {
        
        case 9//90~99
            
            if(point >= 95) {
                System.out.println("A+ํ•™์ ์ž…๋‹ˆ๋‹ค");
            } else {
                System.out.println("Aํ•™์ ์ž…๋‹ˆ๋‹ค");
            }
            
            break;//switch๋ฅผ ๋น ์ ธ๋‚˜๊ฐ€๋ผ
        case 8:
            System.out.println("Bํ•™์ ์ž…๋‹ˆ๋‹ค");
            break;
        case 7:
            System.out.println("Cํ•™์ ์ž…๋‹ˆ๋‹ค");
            break;
        case 6:
            System.out.println("Dํ•™์ ์ž…๋‹ˆ๋‹ค");
            break;
        default :
            if(point > 100) {
                System.out.println("์ž˜๋ชป๋œ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค");
            } else {
                System.out.println("Fํ•™์ ์ž…๋‹ˆ๋‹ค");
            }
            
            
            break;
        }
    }
}
 
 
 
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
31
32
33
34
 
public class SwitchEx3 {
 
    public static void main(String[] args) {
        
        String[] greet = {"์•ˆ๋…•","ํ—ฌ๋กœ""๋‹ˆํ•˜์˜ค""#$%"}; 
        /*        
        String[] greet1 = new String[4];
        greet1[0] = "์•ˆ๋…•";
        .
        .
        */
    
        int r = (int)(Math.random() * greet.length);
        
        System.out.println("์„ ํƒ๋œ๋‹จ์–ด" + greet[r]);
        
        switch (greet[r]) {
        case "์•ˆ๋…•":
            System.out.println("ํ•œ๊ตญ์–ด");
            break;
        case "ํ—ฌ๋กœ":
            System.out.println("์˜์–ด");
            break;
        case "๋‹ˆํ•˜์˜ค":
            System.out.println("์ค‘๊ตญ์–ด");
            break;
        default:
            System.out.println("์™ธ๊ณ„์–ด");
            break;
        }
    }
}
 
 
 

 

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
public class SwitchCalculator {
 
    public static void main(String[] args) {
        
        if(args.length != 3) {
            System.out.println("๋ฐ์ดํ„ฐ๋ฅผ ๊ณต๋ฐฑ๊ณผ ํ•จ๊ผ 3๊ฐœ ์ „๋‹ฌํ•˜์„ธ์š”");
            System.out.println("์‚ฌ์šฉ ์˜ˆ์‹œ: 5 x 9");
            
            System.exit(0); //๋‹น์žฅ ์ข…๋ฃŒํ•ด๋ผ
        }
 
        String str1 = args[0];
        String str3 = args[2];
        
        //์ž๋ฐ”์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ์ž์—ด์„ ->์ˆซ์ž๋กœ ๋ฐ”๊พธ๋Š” ๊ธฐ๋Šฅ Integer.parseInt(๋ฌธ์ž์—ด)
        int n1 = Integer.parseInt(str1);
        int n2 = Integer.parseInt(str3);
        
        
        
        
        switch(args[1]) {
        
        case "+":
            System.out.println("๋‘ ์ˆ˜์˜ ๋ง์…ˆ์€:" + (n1 + n2));
            break;
        case "-":
            System.out.println("๋‘ ์ˆ˜์˜ ๋บ„์…ˆ์€:" + (n1 - n2));
            break;
        case "x":
            System.out.println("๋‘ ์ˆ˜์˜ ๊ณฑ์…ˆ์€:" + (n1 * n2));
            break;
        case "/":
            System.out.println("๋‘ ์ˆ˜์˜ ๋‚˜๋ˆ—์…ˆ์€:" + (n1 / n2));
            break;
        default:
            System.out.println("์‚ฌ์น™์—ฐ์‚ฐ๊ธฐํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”");
        
        }
        
    }
}
 
 
 

 

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€