-
[์๋ฐ(JAVA)] ์กฐ๊ฑด๋ฌธ(if, switch, case), Object.equals, ๋ฐ๋ณต๋ฌธ(for, while, do-while), break, continueJAVA 2023. 5. 23. 15:28728x90
๐น ์กฐ๊ฑด๋ฌธ
: ํ๋ก๊ทธ๋๋ฐ์์ ํน์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ฐ์ฐ์ ์ํํ๊ณ ์ถ์๋ ์ฌ์ฉ
ex) if, switch, case
IF๋ฌธ
: ํน์ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค๋ฅธ ์ฐ์ฐ์ ์ํํ๊ณ ์ถ์ ๋ ์ฌ์ฉํจ
public static void main(String[] args) { int a = 1; if(a % 2 == 1) { System.out.println("ํ์์ ๋๋ค"); } else if (a % 2 == 0) { System.out.println("์ง์์ ๋๋ค"); } }
1. if(์กฐ๊ฑด๋ฌธ) {}
-> if์ ์๊ดํธ() ์์ ์กฐ๊ฑด์ด true๋ฉด ์ค๊ดํธ {}์์ ์ฐ์ฐ์ ์ํํจ
2. else if (์กฐ๊ฑด๋ฌธ) {}
-> if๋ฌธ์ด ๊ฑฐ์ง์ผ ๊ฒฝ์ฐ else if๋ฌธ์ผ๋ก ์งํ๋จ, ์คํ์กฐ๊ฑด์ if์ ๋์ผํจ
3. else {}
-> else๋ฌธ๋ ๋ง์กฑ๋์ง ์์ ๊ฒฝ์ฐ else๋ก ์งํ
์ค์ if๋ฌธ
boolean flag = true; int number = 2; if (flag) { if (number == 1) { System.out.println("flag ๊ฐ์ true, number ๊ฐ์ 1 ์ ๋๋ค."); // ๋ฏธ์ถ๋ ฅ } else if (number == 2) { System.out.println("flag ๊ฐ์ true, number ๊ฐ์ 2 ์ ๋๋ค."); // flag = true // number=2 ์ถ๋ ฅ } } else { if (number == 1) { System.out.println("flag ๊ฐ์ false, number ๊ฐ์ 1 ์ ๋๋ค."); // ๋ฏธ์ถ๋ ฅ } else if (number == 2) { System.out.println("flag ๊ฐ์ false, number ๊ฐ์ 2 ์ ๋๋ค."); // ๋ฏธ์ถ๋ ฅ } }
โ ์ค์ if๋ฌธ์ ํ์ฉํ ์์(๊ฐ์๋ฐ์๋ณด ๊ฒ์)
public class W04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("A์ ๋ ฅ: "); String aHand = sc.nextLine(); System.out.println("B์ ๋ ฅ: "); String bHand = sc.nextLine(); if (Objects.equals(aHand, "๊ฐ์")) { if (Objects.equals(bHand, "๊ฐ์")){ System.out.println("A์ B๋ ๋น๊ฒผ์ต๋๋ค."); } else if(Objects.equals(bHand, "๋ฐ์")){ System.out.println("B๊ฐ ์ด๊ฒผ์ต๋๋ค."); } else if(Objects.equals(bHand, "๋ณด")){ System.out.println("A๊ฐ ์ด๊ฒผ์ต๋๋ค."); } else { System.out.println("B๊ฐ ์ด์ํ ๊ฐ์ ์ ๋ ฅํ์ต๋๋ค."); } } else if (Objects.equals(aHand, "๋ฐ์")) { if (Objects.equals(bHand, "๋ฐ์")) { System.out.println("A์ B๋ ๋น๊ฒผ์ต๋๋ค."); } else if (Objects.equals(bHand, "๋ณด")) { System.out.println("B๊ฐ ์ด๊ฒผ์ต๋๋ค."); } else if (Objects.equals(bHand, "๊ฐ์")) { System.out.println("A๊ฐ ์ด๊ฒผ์ต๋๋ค."); } else { System.out.println("B๊ฐ ์ด์ํ ๊ฐ์ ์ ๋ ฅํ์ต๋๋ค."); } } else if (Objects.equals(aHand, "๋ณด")) { if (Objects.equals(bHand, "๋ณด")) { System.out.println("A์ B๋ ๋น๊ฒผ์ต๋๋ค."); } else if (Objects.equals(bHand, "๊ฐ์")) { System.out.println("B๊ฐ ์ด๊ฒผ์ต๋๋ค."); } else if (Objects.equals(bHand, "๋ฐ์")) { System.out.println("A๊ฐ ์ด๊ฒผ์ต๋๋ค."); } else { System.out.println("B๊ฐ ์ด์ํ ๊ฐ์ ์ ๋ ฅํ์ต๋๋ค."); } } else { System.out.println("A๊ฐ ์ด์ํ ๊ฐ์ ์ ๋ ฅํ์ต๋๋ค."); } } }
โญObject.equals(์ข, ์ฐ)
- ๋ ๊ฐ์ ๊ฐ์ ๋น๊ตํ๋ ๋ฉ์๋ -> Object.equals(์ข, ์ฐ)
- ์ข, ์ฐ ๊ฐ์ ๋น๊ตํ์ฌ ๊ฐ์ผ๋ฉด true, ๋ค๋ฅด๋ฉด false ๋ฐํswitch ๋ฌธ
: ๋ชจ๋ ์กฐ๊ฑด๋ค์ ํ์ค๋ก ๋์ดํ๋ฉด์ ๋ฌด์จ ์กฐ๊ฑด์๋ ์ด๋ค ์ญํ ์ ํ๋์ง ํ ๋์ ๋ณด์ด๊ธฐ ๋๋ฌธ์ if๋ฌธ๋ณด๋ค ๊ฐ๋ ์ฑ์ด ์ข์
ํํ
: switch(ํผ์ฐ์ฐ์ or ์กฐ๊ฑด) {
case(์กฐ๊ฑด) : (์ฐ์ฐ)
break;
...
[default: (์ฐ์ฐ)] - ์๋ต๊ฐ๋ฅ
}switch ํผ์ฐ์ฐ์๊ฐ case ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด case: ๋ค์ ๋ช ์๋์ด ์๋ ์ฐ์ฐ์ ์ํํ๋ค.
case์กฐ๊ฑด์ ์ฌ๋ฌ๊ฐ๋ฅผ ์ค์ ํ ์ ์๋ค
๋จ, ๊ฐ case์ฐ์ฐ๋ฌธ์ ๋ง์ง๋ง์๋ break๋ฅผ ๊ผญ ๋ฃ์ด์ค์ผ ํ๋ค.
(๋ฏธ๊ธฐ์ ์ ์กฐ๊ฑด์ ๋ง์กฑํด๋ switch๋ฌธ์ ๋ชจ๋ ๋ธ๋ญ์ ์ ๋ถ ์คํ ํ ์ข ๋ฃ๋จ)
default: (์ฐ์ฐ) ์ ๋ช ์ํ์ฌ case๋ฅผ ๋ชจ๋ ๋ง์กฑํ์ง ์์๋ ์ํํ ์ฐ์ฐ์ ์ค์ ํ๋ค
โ if๋ฌธ์ else์ญํ
default๋ฌธ์ด ์๋ค๋ฉด ์๋ต ๊ฐ๋ฅ
โ switch๋ฌธ ์์
public class W05 { public static void main(String[] args) { int month = 8; String monthString = ""; // ์ด๊ธฐ๊ฐ ์ง์ switch (month) { case 1: // month๊ฐ์ด 1์ธ์ง ๋น๊ต monthString = "1์"; break; case 2: monthString = "2์"; break; case 3: monthString = "3์"; break; case 4: monthString = "4์"; break; case 5: monthString = "5์"; break; case 6: monthString = "6์"; break; case 7: monthString = "7์"; break; case 8: monthString = "8์"; break; case 9: monthString = "9์"; break; case 10: monthString = "10์"; break; case 11: monthString = "11์"; break; case 12: monthString = "12์"; break; default: monthString = "์ ์ ์์"; //default๋ฌธ์๋ break;๋ฅผ ๊ตณ์ด ์์จ๋ ๋จ } System.out.println(monthString); // 8์ ์ถ๋ ฅ } }
if-switch๋ฌธ ์ฐจ์ด์
โพ ์ฐจ์ด์ 1. ๋ณตํฉ์กฐ๊ฑด ์ง์์ฌ๋ถ
โฝ if๋ฌธ์ ๋ณตํฉ์กฐ๊ฑด์ ์ง์ํ๋ค.
โป ๋ณตํฉ์กฐ๊ฑด: ๊ดํธ()์์ ์กฐ๊ฑด์ ์ฌ๋ฌ๊ฐ ์ง์ ํ ์ ์๋ค
โป switch ๋ฌธ์ ํผ์ฐ์ฐ์ ํ๊ฐ์ ๋ํ ์กฐ๊ฑด๋ง ์งํ๊ฐ๋ฅ
ex) switch(month) { }
โพ ์ฐจ์ด์ 1. ์ฝ๋ ์ค๋ณต
โฝ if ๋ฌธ์ ์๋์ ์ผ๋ก ์ฝ๋์ค๋ณต์ด ๋ง๊ณ switch๋ฌธ์ ์ ๋ค
โฝ ์ฝ๋๋น๊ต์์
โ switch๋ฌธ์ ifํํ ๋ณํ ์์
// if ๋ก ๋ณํ if (month == 1) { monthString = "1์"; } else if (month == 2) { monthString = "2์"; } else if (month == 3) { monthString = "3์"; } else if (month == 4) { monthString = "4์"; } else if (month == 5) { monthString = "5์"; } else if (month == 6) { monthString = "6์"; } else if (month == 7) { monthString = "7์"; } else if (month == 8) { monthString = "8์"; } else if (month == 9) { monthString = "9์"; } else if (month == 10) { monthString = "10์"; } else if (month == 11) { monthString = "11์"; } else if (month == 12) { monthString = "12์"; } else { monthString = "์์ ์์"; } System.out.println(monthString); // 8์ ์ถ๋ ฅ
๐น ๋ฐ๋ณต๋ฌธ
: ํน์ ์กฐ๊ฑด์ ๋ฐ๋ณตํด์ ์ฐ์ฐ์ ์ํํ๊ณ ์ถ์ ๋ ์ฌ์ฉ
(์ด๋ค ๋ณ์๊ฐ ~๊ฐ ๋ ๋ ๊น์ง ์งํ)
ex) for, while, do-while
for ๋ฌธ
: ํน์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ฐ์ฐ์ ๋ฐ๋ณตํด์ ์ํํ๊ณ ์ถ์๋ ์ฌ์ฉ
-> ์๋ชปํ๋ฉด ๋ฌดํ๋ฃจํ์ ๋น ์ง ์ ์๋ค.
ํํ: for (์ด๊ธฐ๊ฐ; ์กฐ๊ฑด๋ฌธ; ์ฆ๊ฐ์ฐ์ฐ) { (์ฐ์ฐ) }
- ํน์ ์กฐ๊ฑด์ ์ด๊ธฐ๊ฐ๊ณผ ์กฐ๊ฑด๋ฌธ์ ํตํด์ ์ ์ํจ
- ๋ฐ๋ณตํ ๋๋ง๋ค ๊ฐ์ ์ฆ๊ฐ ์ํค๋ ค๋ฉด ์ฆ๊ฐ์ฐ์ฐ์ด ํ์ํจ.
- ์ด๊ธฐ๊ฐ์ด ์กฐ๊ฑด๋ฌธ์ ๋ง์กฑํ ๋๊น์ง (์ฐ์ฐ)์ ์ํํ๋ฉด์ ํ์ฐจ๋ง๋ค ์ฆ๊ฐ์ฐ์ฐ์ ์ํํ๋ค.
โ for๋ฌธ ์์
public static void main(String[] args) { // for ๋ฌธ ๊ธฐ๋ณธํํ // for (์ด๊ธฐ๊ฐ; ์กฐ๊ฑด๋ฌธ; ์ฆ๊ฐ์ฐ์ฐ) { (์ฐ์ฐ) } for(int i=0; i < 4; i++) { // ๋ณ์ i ๊ฐ์ 0 ~ 3 ๊น์ง ๋ฐ๋ณต System.out.println(i + "๋ฒ์งธ ์ถ๋ ฅ"); // i ๋ณ์์ ๋ฌธ์์ด ํฉ์น๊ธฐ } } }
๐ ํฅ์๋ for๋ฌธ
: ๋ฐฐ์ด๊ณผ ํจ๊ป ์ฌ์ฉํ๋ฉฐ ์ฝ๋์ฌ์ฉ์ด ๊ฐ๋จํด์ง๋ค
ํํ:
1. ๋ฐฐ์ด์ ์ธ
2. for(ํํ ๋ณ์๋ช : ์ ์ธ๋ ๋ฐฐ์ด) {}
* ์ธ๋ฏธ์ฝ๋ก (;)์ด ์๋๋ผ :(์์ )์ผ๋ก ์ฌ์ฉํด์ผ๋จโ ํฅ์๋ for๋ฌธ ์์
public static void main(String[] args) { int[] numbers = {3, 6, 9, 12, 15}; for (int number: numbers) { System.out.println(number); } }
numbers๋ก ์ ์ธ๋ ๋ฐฐ์ด๊ฐ์ด ํ๋์ฉ ๋ค์ด์ค๋ฉด์ number์ ํ๋์ฉ ์ฐ์ด๊ฒ ๋จ
while๋ฌธ
: for ๋ฌธ๊ณผ ๋์ผํ๊ฒ ์ฐ์ฐ์ ๋ฐ๋ณต์ํํ๋, ์ด๊ธฐ๊ฐ ์์ด ์กฐ๊ฑด๋ฌธ๋ง ๋ช ์ํ์ฌ ๋ฐ๋ณตํ๋ ์ฐจ์ด๊ฐ ์๋ค.
ํํ:
while(์กฐ๊ฑด๋ฌธ) { (์ฐ์ฐ) } -> (whileํ)while๋ฌธ์ ํนํ ๋ฌดํ๋ฃจํ๋ฅผ ์กฐ์ฌํด์ผํจ!
โ whlie๋ฌธ ์์
public static void main(String[] args) { // while ๋ฌธ int number = 0; while (number < 3) { // number < 3์ธ ๋์๊น์ง ๋ฐ๋ณต์งํ number++; System.out.println(number + "์ถ๋ ฅ"); } }
while๋ฌธ์ ()์์ ์กฐ๊ฑด๋ง ๋ค์ด๊ฐ
-> 3๋ณด๋ค ์์ ๊ฐ์ ๋ํ ์ฆ๊ฐ ๋ถ๋ถ์ด๋ ์ถ๊ฐ์ ์ผ๋ก ํ์ํ ๋ก์ง์ ๋ชจ๋ { }์์ ์ ์ด์ค์ผํจ
do-while๋ฌธ
: while๋ฌธ๊ณผ ๋์ผํ๋
while์ ์กฐ๊ฑด์ ์ต์ด์ ๋ง์กฑํ์ง ์๋๋ผ๋ do์์ ์์ ์ด ๋ฌด์กฐ๊ฑด ์ ํ๋์ด์ผ ํ ๋ ์ฌ์ฉ๋๋ค
ํํ: do {
(์ฐ์ฐ) // ์ด ๋ก์ง์ ๋จผ์ ์ํํจ
} while(์กฐ๊ฑด๋ฌธ)โ do-while๋ฌธ ์์
// do-while ๋ฌธ int number = 4; do { System.out.println(number + "์ถ๋ ฅ"); } while(number < 3); // ์ฐ์ฐ์ ํ๋ฒ ์ํ ํ ์กฐ๊ฑด๋ฌธ ์ฒดํฌ // 4์ถ๋ ฅ // ์ดํ ๋ฐ๋ณต๋ฌธ ์ข ๋ฃ
๐ธ break๋ช ๋ น
- ๊ฐ์ฅ ๊ฐ๊น์ด ๋ธ๋ก์ for๋ฌธ, while๋ฌธ, switch๋ฌธ์ ์ค๋จํ ๋ ์ฌ์ฉํจ (๋ฐ๋ณต or ์กฐ๊ฑด๋ฌธ ํ์ถ)
// break ๋ช ๋ น int number = 0; while(number < 3) { number++; if (number == 2) { break; // 2์ผ๋ ๋ฐ๋ณต ์ค๋จ } System.out.println(number + "์ถ๋ ฅ"); } // ์ถ๋ ฅ 1์ถ๋ ฅ
๐ธ continue
- for๋ฌธ, while๋ฌธ, switch๋ฌธ์ ํด๋น ์์๋ฅผ ํจ์คํ๊ณ ์ถ์๋ ์ฌ์ฉํจ (๋ฐ๋ณต or ์กฐ๊ฑด๋ฌธ ์์๋๊น)
// continue ๋ช ๋ น int number = 0; while(number < 3) { number++; if (number == 2) { continue; // 2์ผ๋ ๋ฐ๋ณต ํจ์ค } System.out.println(number + "์ถ๋ ฅ"); } // ์ถ๋ ฅ 1์ถ๋ ฅ 3์ถ๋ ฅ
โ ๊ตฌ๊ตฌ๋จ ์์1 (์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ) - ๊ธฐ๋ณธํ
public static void main(String[] args) { // ๊ตฌ๊ตฌ๋จ ์งํ // ํํ: 2๊ณฑํ๊ธฐ 2๋ 4์ ๋๋ค. for(int i=2;i<=9;i++){ for(int j=2;j<=9;j++){ System.out.println(i+"๊ณฑํ๊ธฐ "+j+"๋ "+(i*j)+"์ ๋๋ค."); } } }
โ ๊ตฌ๊ตฌ๋จ ์์2 (์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ) - continue ์ฌ์ฉ
1.๋ด๊ฐ ์ ๋ ฅํ ๋จ์ ์ ์ธํ๊ณ ์ถ๋ ฅ (passNum)
public static void main(String[] args) { //์ ๋ ฅํ ๋จ์ ์ ์ธ ํ ์ถ๋ ฅ Scanner sc = new Scanner(System.in); int passNum = sc.nextInt(); for(int i=2;i<=9;i++){ if(i==passNum) continue; for(int j=2;j<=9;j++){ System.out.println(i+"๊ณฑํ๊ธฐ "+j+"๋ "+(i*j)+"์ ๋๋ค."); } } }
2. ์ ๋ ฅํ ๋จ๋ง ์ถ๋ ฅ(targetNum)
public static void main(String[] args) { //์ ๋ ฅํ ๋จ๋ง ์ถ๋ ฅ Scanner sc = new Scanner(System.in); int targetNum = sc.nextInt(); for(int i=2;i<=9;i++){ if(i!=targetNum) continue; for(int j=2;j<=9;j++){ System.out.println(i+"๊ณฑํ๊ธฐ "+j+"๋ "+(i*j)+"์ ๋๋ค."); } } }
728x90'JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ