Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[25] 230203 Java Ch. 4 ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ: 1. ์กฐ๊ฑด๋ฌธ: if๋ฌธ, switch๋ฌธ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 25์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Spring Framework, Java

[25] 230203 Java Ch. 4 ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ: 1. ์กฐ๊ฑด๋ฌธ: if๋ฌธ, switch๋ฌธ [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 25์ผ]

yjyuwisely 2023. 2. 2. 13:02

230203 Thu 25th class

Ch. 4 ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ
์ง„๋„: p. 134 ~ (๊ต์žฌ: ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ”, ์ €์ž: ์‹ ์šฉ๊ถŒ)

 

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ”

๋…ํ•™์œผ๋กœ ์ž๋ฐ”๋ฅผ ๋ฐฐ์šฐ๋Š” ์ž…๋ฌธ์ž๊ฐ€ ‘๊ผญ ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ œ๋Œ€๋กœ’ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ–ˆ๋‹ค. ‘๋ฌด์—‡์„’ ‘์–ด๋–ป๊ฒŒ’ ํ•™์Šตํ•ด์•ผ ํ• ์ง€ ์กฐ์ฐจ ๋ชจ๋ฅด๋Š” ์ž…๋ฌธ์ž์˜ ๋ง‰์—ฐํ•œ ๋งˆ์Œ์„ ์‚ดํŽด, ๊ณผ์™ธ ์„ ์ƒ๋‹˜์ด

www.aladin.co.kr

 

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค. 


์ง€๋‚œ ์ˆ˜์—… ๋•Œ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ

Scanner

์ฐธ๊ณ : https://www.w3schools.com/java/java_user_input.asp

Java User Input

The Scanner class is used to get user input, and it is found in the java.util package.

To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:

์˜ˆ์ œ)

import java.util.Scanner; // import the Scanner class 

class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);
    String userName;
    
    // Enter username and press Enter
    System.out.println("Enter username"); 
    userName = myObj.nextLine();   
       
    System.out.println("Username is: " + userName);        
  }
}

๊ฒฐ๊ณผ) 

Enter username
Hi
Username is: Hi

Input Types

In the example above, we used the nextLine() method, which is used to read Strings. To read other types, look at the table below:

Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user

์˜ˆ์ œ)

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);

    System.out.println("Enter name, age and salary:");

    // String input
    String name = myObj.nextLine();

    // Numerical input
    int age = myObj.nextInt();
    double salary = myObj.nextDouble();

    // Output input by user
    System.out.println("Name: " + name); 
    System.out.println("Age: " + age); 
    System.out.println("Salary: " + salary); 
  }
}

๊ฒฐ๊ณผ)

Enter name, age and salary:
Hi
10
120000
Name: Hi
Age: 10
Salary: 120000

Ch. 4 ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ

์ฑ…์˜ ๋ชฉ์ฐจ
04-1 ์กฐ๊ฑด๋ฌธ: if๋ฌธ, switch๋ฌธ
04-2 ๋ฐ˜๋ณต๋ฌธ: for๋ฌธ, while๋ฌธ, do-while๋ฌธ

4.1 ์กฐ๊ฑด๋ฌธ: if๋ฌธ, switch๋ฌธ

1) if๋ฌธ p. 135

 if (์กฐ๊ฑด์‹){...}else{...}
์กฐ๊ฑด์‹์—๋Š” true ๋˜๋Š” false ๊ฐ’์„ ์‚ฐ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ์—ฐ์‚ฐ์‹์ด๋‚˜, boolean ํƒ€์ž… ๋ณ€์ˆ˜๊ฐ€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
์กฐ๊ฑด์‹์ด true์ด๋ฉด ๋ธ”๋ก์„ ์‹คํ–‰ํ•˜๊ณ , false์ด๋ฉด ๋ธ”๋ก์„ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
๊ด„ํ˜ธ {} ๋ธ”๋ก์€ ์ƒ๋žตํ•˜์ง€ ์•Š๊ณ  ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. 

์˜ˆ์ œ) 

public class P136IfExample {
	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์ž…๋‹ˆ๋‹ค."); //๋“ค์—ฌ์“ฐ๊ธฐ๋งŒ ๋˜์—ˆ์„ ๋ฟ if๋ฌธ๊ณผ ๊ด€๋ จ์ด ์—†์œผ๋ฏ€๋กœ ์ถœ๋ ฅ๋œ๋‹ค. 	
	}
}

๊ฒฐ๊ณผ)

์ ์ˆ˜๊ฐ€ 90๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค.
๋“ฑ๊ธ‰์€ A์ž…๋‹ˆ๋‹ค.
๋“ฑ๊ธ‰์€ B์ž…๋‹ˆ๋‹ค.

2) if-else๋ฌธ p. 136

if (์กฐ๊ฑด์‹){...}else{...}
์กฐ๊ฑด์‹์ด true๊ฐ€ ๋˜๋ฉด if ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๋ฅผ ์‹คํ–‰ํ•˜๊ณ , false๊ฐ€ ๋˜๋ฉด else ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€์‹คํ–‰ํ•œ๋‹ค.

์˜ˆ์ œ)

public class P137IfElseExample {
	public static void main(String[] args) {
		int score = 93;
		if(score>=90) {
			System.out.println("์ ์ˆ˜๊ฐ€ 90๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค.");
			System.out.println("๋“ฑ๊ธ‰์€ A์ž…๋‹ˆ๋‹ค.");
		}else {
			System.out.println("์ ์ˆ˜๊ฐ€ 90๋ณด๋‹ค ์ž‘์Šต๋‹ˆ๋‹ค."); 
			System.out.println("๋“ฑ๊ธ‰์€ B์ž…๋‹ˆ๋‹ค."); 	
	}
	}
}

๊ฒฐ๊ณผ)

์ ์ˆ˜๊ฐ€ 90๋ณด๋‹ค ํฝ๋‹ˆ๋‹ค.
๋“ฑ๊ธ‰์€ A์ž…๋‹ˆ๋‹ค.

3) if-else if-else p. 138

 if (์กฐ๊ฑด์‹1) {...} else if(์กฐ๊ฑด์‹2) {...} else {...}
์กฐ๊ฑด์‹ 1์ด true๊ฐ€ ๋˜๋ฉด if ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ์กฐ๊ฑด์‹2๊ฐ€ true๊ฐ€ ๋˜๋ฉด else if ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
์กฐ๊ฑด์‹1๊ณผ ์กฐ๊ฑด์‹2๊ณผ ๋ชจ๋‘ false๊ฐ€ ๋˜๋ฉด  else ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

if (์กฐ๊ฑด์‹1) {
	//์กฐ๊ฑด์‹1์ด true
    ์‹คํ–‰๋ฌธ A
} else if (์กฐ๊ฑด์‹2) {
	//์กฐ๊ฑด์‹2๊ฐ€ true
    ์‹คํ–‰๋ฌธ B
} else {
	์‹คํ–‰๋ฌธ C
}
	์‹คํ–‰๋ฌธ D

1. ์กฐ๊ฑด์‹1์ด true์ด๋ฉด A→D
2. ์กฐ๊ฑด์‹1์ด false์ด๋ฉด 
     2.1 ์กฐ๊ฑด์‹2๊ฐ€ true์ด๋ฉด B→D
     2.2 ์กฐ๊ฑด์‹2๊ฐ€ false์ด๋ฉด C→D

์˜ˆ์ œ)

public class P139IfElseIfExample {
	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~90์ž…๋‹ˆ๋‹ค.");
			System.out.println("๋“ฑ๊ธ‰์€ B์ž…๋‹ˆ๋‹ค.");
		}else if(score >= 70) {
			System.out.println("์ ์ˆ˜๊ฐ€ 70~80์ž…๋‹ˆ๋‹ค.");
			System.out.println("๋“ฑ๊ธ‰์€ C์ž…๋‹ˆ๋‹ค.");
		}else {
		System.out.println("์ ์ˆ˜๊ฐ€ 60~70์ž…๋‹ˆ๋‹ค.");
		System.out.println("๋“ฑ๊ธ‰์€ D์ž…๋‹ˆ๋‹ค.");
	}
}
}

๊ฒฐ๊ณผ) 

์ ์ˆ˜๊ฐ€ 70~80์ž…๋‹ˆ๋‹ค.
๋“ฑ๊ธ‰์€ C์ž…๋‹ˆ๋‹ค.

๋žœ๋ค Math.random() 

ex) ํšŒ์› ๊ฐ€์ž… ๋•Œ ์ˆซ์ž ์ธ์ฆ

start๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” n๊ฐœ์˜ ์ •์ˆ˜ ์ค‘์—์„œ ์ž„์˜์˜ ์ •์ˆ˜ ํ•˜๋‚˜๋ฅผ ์–ป๊ธฐ ์œ„ํ•œ ์—ฐ์‚ฐ์‹

int num = (int) (Math.random() * n) + start;

์˜ˆ์ œ 1) ๋กœ๋˜ ๋ฒˆํ˜ธ (1~45) ํ•˜๋‚˜๋ฅผ ๋ฝ‘๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์‹

int num = (int) (Math.random() * 45) + 1;

์˜ˆ์ œ 1.1) ๋žœ๋ค ๋ฒˆํ˜ธ 6๊ฐœ ๋งŒ๋“ ๋‹ค.

import java.util.Random;
public class P140Lottery {
	public static void main(String[] args) {    
        Random random = new Random();		//๋žœ๋ค ํ•จ์ˆ˜ ์„ ์–ธ
		int createNum = 0;  			//1์ž๋ฆฌ ๋‚œ์ˆ˜
		String ranNum = ""; 			//1์ž๋ฆฌ ๋‚œ์ˆ˜ ํ˜•๋ณ€ํ™˜ ๋ณ€์ˆ˜
        int letter    = 6;			//๋‚œ์ˆ˜ ์ž๋ฆฟ์ˆ˜:6
		String resultNum = "";  		//๊ฒฐ๊ณผ ๋‚œ์ˆ˜
		
		for (int i=0; i<letter; i++) { 		
			createNum = random.nextInt(46);		//1๋ถ€ํ„ฐ 45๊นŒ์ง€ ์˜ฌ ์ˆ˜ ์žˆ๋Š” 1์ž๋ฆฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ
			ranNum =  Integer.toString(createNum);  //1์ž๋ฆฌ ๋‚œ์ˆ˜๋ฅผ String์œผ๋กœ ํ˜•๋ณ€ํ™˜
			resultNum += "["+ranNum+"]";			//์ƒ์„ฑ๋œ ๋‚œ์ˆ˜(๋ฌธ์ž์—ด)์„ ์›ํ•˜๋Š” ์ˆ˜(letter)๋งŒํผ ๋”ํ•˜๋ฉฐ ๋‚˜์—ด
		}	  	
            System.out.println("๋žœ๋ค ๋ฒˆํ˜ธ 6๊ฐœ: "+resultNum);
	}
}

๊ฒฐ๊ณผ)

๋žœ๋ค ๋ฒˆํ˜ธ 6๊ฐœ: [33][25][31][22][35][2]

์˜ˆ์ œ 2)

public class P140IfDiceExample {
	public static void main(String[] args) {
	// ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” 1~6 ์ค‘์—์„œ ํ•˜๋‚˜์˜ ์ˆ˜๋ฅผ ๋ฌด์ž‘์œ„๋กœ ๋ฝ‘์•„์„œ ์ถœ๋ ฅ 
	int num = (int)(Math.random() * 6) + 1; //6๊ฐœ์˜ ์ •์ˆ˜, start=1
	//์ฃผ์‚ฌ์œ„์˜ ์ˆซ์ž(num)๊ฐ€ 1์ด๋ฉด
	if(num==1) {
		//1๋ฒˆ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ๋ผ๊ณ  ์ถœ๋ ฅ
		System.out.println("1๋ฒˆ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.");
	} else if(num==2) {
		System.out.println("2๋ฒˆ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.");
	} else if(num==2) {
		System.out.println("3๋ฒˆ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.");
	} else if(num==2) {
		System.out.println("4๋ฒˆ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.");
	} else if(num==2) {
		System.out.println("5๋ฒˆ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.");
	} else {
		System.out.println("6๋ฒˆ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.");
	}
}
}

๊ฒฐ๊ณผ) 

6๋ฒˆ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.

4) switch๋ฌธ p. 141

 switch(๋ณ€์ˆ˜){case ๊ฐ’1: ... case ๊ฐ’2: ... default: ... }

๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๊ฐ’1์ด๋ฉด ์ฒซ ๋ฒˆ์งธ case ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ๊ฐ’2์ด๋ฉด ๋‘ ๋ฒˆ์งธ case ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ฐ’1๊ณผ ๊ฐ’2๊ฐ€ ๋ชจ๋‘ ์•„๋‹ˆ๋ฉด default ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. default๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.

switch( ๋ณ€์ˆ˜ ) { 
    case ๊ฐ’1: //๋ณ€์ˆ˜๊ฐ€ ๊ฐ’1์ผ ๊ฒฝ์šฐ
    ์‹คํ–‰๋ฌธA
    break;
    
    case ๊ฐ’2: //๋ณ€์ˆ˜๊ฐ€ ๊ฐ’2์ผ ๊ฒฝ์šฐ
    ์‹คํ–‰๋ฌธB
    break;
    
    default: //๋ณ€์ˆ˜๊ฐ€ ๊ฐ’1,๊ฐ’2 ๋ชจ๋‘ ์•„๋‹ ๊ฒฝ์šฐ (์ƒ๋žต ๊ฐ€๋Šฅ)
    ์‹คํ–‰๋ฌธC
}

์˜ˆ์ œ 1)

public class P143SwitchExample {
	public static void main(String[] args) {
	// ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ ค์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” 1~6 ์ค‘์—์„œ ํ•˜๋‚˜์˜ ์ˆ˜๋ฅผ ๋ฌด์ž‘์œ„๋กœ ๋ฝ‘์•„์„œ ์ถœ๋ ฅ 
	int num = (int)(Math.random() * 6) + 1; //6๊ฐœ์˜ ์ •์ˆ˜, start=1
	switch(num) {
	case 1: //์ฃผ์‚ฌ์œ„์˜ ์ˆซ์ž(num)๊ฐ€ 1์ด๋ฉด
		//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;
	}
}
}

๊ฒฐ๊ณผ) 

3๋ฒˆ์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.

case ๋์— break๊ฐ€ ๋ถ™์–ด ์žˆ๋Š” ์ด์œ ๋Š” ๋‹ค์Œ case๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  switch ๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ€๊ธฐ ์œ„ํ•ด์„œ์ด๋‹ค. 
break๊ฐ€ ์—†๋‹ค๋ฉด ๋‹ค์Œ case๊ฐ€ ์—ฐ๋‹ฌ์•„ ์‹คํ–‰๋œ๋‹ค. 


๋งˆ๋ฌด๋ฆฌ
4๊ฐ€์ง€ ํ‚ค์›Œ๋“œ๋กœ ๋๋‚ด๋Š” ํ•ต์‹ฌ ํฌ์ธํŠธ p. 145

1) if๋ฌธ if (์กฐ๊ฑด์‹){...}else{...}
์กฐ๊ฑด์‹์ด true๊ฐ€ ๋˜๋ฉด ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

2) if-else๋ฌธ: if (์กฐ๊ฑด์‹){...}else{...}
์กฐ๊ฑด์‹์ด true๊ฐ€ ๋˜๋ฉด if ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๋ฅผ ์‹คํ–‰ํ•˜๊ณ , false๊ฐ€ ๋˜๋ฉด else ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

3) if-else if-else: if (์กฐ๊ฑด์‹1) {...} else if(์กฐ๊ฑด์‹2) {...} else {...}
์กฐ๊ฑด์‹1์ด true๊ฐ€ ๋˜๋ฉด if ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ์กฐ๊ฑด์‹2๊ฐ€ true๊ฐ€ ๋˜๋ฉด else if ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
์กฐ๊ฑด์‹1๊ณผ ์กฐ๊ฑด์‹2๊ฐ€ ๋ชจ๋‘ false๊ฐ€ ๋˜๋ฉด else ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

4) switch๋ฌธ: switch(๋ณ€์ˆ˜){case ๊ฐ’1: ... case ๊ฐ’2: ... default: ... }
๋ณ€์ˆ˜์˜ ๊ฐ’์ด ๊ฐ’1์ด๋ฉด ์ฒซ ๋ฒˆ์งธ case ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ๊ฐ’2์ด๋ฉด ๋‘ ๋ฒˆ์งธ case ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
๊ฐ’1๊ณผ ๊ฐ’2๊ฐ€ ๋ชจ๋‘ ์•„๋‹ˆ๋ฉด default ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

//switch๋ฌธ
switch( ๋ณ€์ˆ˜ ){
//๋ณ€์ˆ˜๊ฐ€ ๊ฐ’1์ผ ๊ฒฝ์šฐ
case ๊ฐ’1:
์‹คํ–‰๋ฌธA

break;
//๋ณ€์ˆ˜๊ฐ€ ๊ฐ’2์ผ ๊ฒฝ์šฐ
case ๊ฐ’2:
์‹คํ–‰๋ฌธB

break;
//๋ณ€์ˆ˜๊ฐ€ ๊ฐ’1, ๊ฐ’2 ๋ชจ๋‘ ์•„๋‹ ๊ฒฝ์šฐ
default;
์‹คํ–‰๋ฌธC
}

728x90
๋ฐ˜์‘ํ˜•
Comments