JAVA ๊ณต๋ถ€ํ•˜๊ธฐ(4) ๐Ÿง‘๐Ÿปโ€๐Ÿ’ป

33952 ๋‹จ์–ด JavaJava

1. ์กฐ๊ฑด๋ฌธ

์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•˜๋ฉด ์กฐ๊ฑด์˜ ์ฐธ, ๊ฑฐ์ง“์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ž๋ฐ”์˜ ์กฐ๊ฑด๋ฌธ์€ C/C++ ์™€ ๊ฑฐ์˜ ๋™์ผํ•˜๋ฉฐ ํฌ๊ฒŒ if-else ๋ฌธ, switch ๋ฌธ์œผ๋กœ ๋ถ„๋ฅ˜๋œ๋‹ค.

1.(1) ๋‹จ์ˆœ if ๋ฌธ

๋‹จ์ˆœ if ๋ฌธ์˜ ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•˜๋Š” ์‚ฌ๋ก€๋Š” ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค.

if(n%2 == 0){
    System.out.print(n);
    System.out.println("์€ ์ง์ˆ˜์ž…๋‹ˆ๋‹ค.");
}
if(score >= 80 && score <= 89)
    System.out.println("ํ•™์ ์€ B์ž…๋‹ˆ๋‹ค.")

'์กฐ๊ฑด์‹'์€ ๋น„๊ต ์—ฐ์‚ฐ์ด๋‚˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์˜ ํ˜ผํ•ฉ๋œ ์‹์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ ๊ฒฐ๊ณผ๋Š” ๋ถˆ๋ฆฐ ๊ฐ’์ด๋‹ค. ์กฐ๊ฑด์‹์ด ์ฐธ(true)์ด๋ฉด if ๋‚ด๋ถ€์˜ '์‹คํ–‰ ๋ฌธ์žฅ'์ด ์‹คํ–‰๋˜๋ฉฐ, ๊ฑฐ์ง“(false)์ด๋ฉด if ๋ฌธ์„ ๋ฒ—์–ด๋‚œ๋‹ค.

'์กฐ๊ฑด์‹'์€ ๋…ผ๋ฆฌ ํƒ€์ž… ๋ณ€์ˆ˜ ํ•˜๋‚˜๋งŒ์œผ๋กœ ๊ฐ€๋Šฅํ•˜๋ฉฐ, '์‹คํ–‰ ๋ฌธ์žฅ'์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ค‘๊ด„ํ˜ธ({ }) ์‚ฌ์ด์— ๋ฌถ์–ด ํ•œ๊บผ๋ฒˆ์— ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ์ค‘๊ด„ํ˜ธ({ }) ์‚ฌ์ด์— ๋ฌถ์ธ ์—ฌ๋Ÿฌ '์‹คํ–‰ ๋ฌธ์žฅ'์„ ๋ธ”๋ก์ด๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, ํ•˜๋‚˜์˜ ๋‹จ์œ„์ฒ˜๋Ÿผ ๋‹ค๋ฃจ์–ด์ง„๋‹ค. '์‹คํ–‰ ๋ฌธ์žฅ'์ด ํ•œ ๋ฌธ์žฅ๋ฟ์ด๋ฉด ์ค‘๊ด„ํ˜ธ({ })๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

1.(2) if-else ๋ฌธ

if-else ๋ฌธ์€ if์˜ ์กฐ๊ฑด์‹์ด ์ฐธ์ธ ๊ฒฝ์šฐ์™€ ๊ฑฐ์ง“์ธ ๊ฒฝ์šฐ์— ์‹คํ–‰ํ•  ๋ฌธ์žฅ์„ ๊ฐ๊ฐ ์ง€์‹œํ•œ๋‹ค. if-else ๋ฌธ์˜ ๊ตฌ๋ฌธ์˜ ์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค. ์กฐ๊ฑด์‹์ด true์ด๋ฉด '์‹คํ–‰๋ฌธ์žฅ 1'์„ ์‹คํ–‰ํ•œ ํ›„ if-else ๋ฌธ์„ ๋ฒ—์–ด๋‚˜๊ณ , false์ด๋ฉด '์‹คํ–‰๋ฌธ์žฅ 2'๋ฅผ ์‹คํ–‰ํ•œ ํ›„ if-else ๋ฌธ์„ ๋ฒ—์ด๋‚œ๋‹ค.

if(score >= 90)
    System.out.println("ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.");
else
    System.out.println("๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.");

1.(2)-1 ์˜ˆ์ œ

import java.util.Scanner;
public class Twenties {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        System.out.println("๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค: ");
        int age = scanner.nextInt();
        if((age>=20)&&(age<30)){
            System.out.println("20๋Œ€์ž…๋‹ˆ๋‹ค. ");
            System.out.println("20๋Œ€๋ผ์„œ ํ•ญ๋ณตํ•ฉ๋‹ˆ๋‹ค");
        }
        else
            System.out.println("20๋Œ€๊ฐ€ ์•„๋นˆ๋‹ค.");

        scanner.close();
    }
}

1.(3) ๋‹ค์ค‘ if-else ๋ฌธ

๋‹ค์ค‘ if-else ๋ฌธ์€ if-else๊ฐ€ ์—ฐ์†๋˜๋Š” ๊ฒƒ์œผ๋กœ ์ „ํ˜•์ ์ธ ๋ชจ์–‘์€ ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค. ์œ„์—์„œ๋ถ€ํ„ฐ ๋น„๊ตํ•˜์—ฌ '์กฐ๊ฑด์‹'์ด ์ฐธ์ธ ๊ฒฝ์šฐ, ํ•ด๋‹นํ•˜๋Š” '์‹คํ–‰ ๋ฌธ์žฅ'์„ ์‹คํ–‰ํ•œ ํ›„ ๋‹ค์ค‘ if-else ์ „์ฒด๋ฅผ ๋ฒ—์–ด๋‚œ๋‹ค.

if (score >= 90){
           grade = 'A';
       }
       else if(score >= 80){
           grade = 'B';
       }
       else if(score >= 70){
           grade = 'C';
       }
       else if(score >= 60){
           grade = 'D';
       }
       else{
           grade = 'F';
       }

1.(3)-(1) ์˜ˆ์ œ

//๋‹ค์ค‘ if-else๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•™์  ๋งค๊ธฐ๊ธฐ

import java.util.Scanner;
public class Grading {
    public static void main(String[] args){
        char grade;
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(0~100)");
        int score = scanner.nextInt(); //์ ์ˆ˜์ฝ๊ธฐ
        if (score >= 90)
            grade = 'A';
        else if (score >= 80)
            grade = 'B';
        else if (score >= 70)
            grade = 'C';
        else if (score >= 60)
            grade = 'D';
        else 
            grade = 'F';
        
        System.out.println("ํ•™์ ์€ " + grade + "์ž…๋‹ˆ๋‹ค.");
        scanner.close();
    }
}

1.(4) ์ค‘์ฒฉ if-else ๋ฌธ

if ๋ฌธ์ด๋‚˜ if-else ๋ฌธ, ํ˜น์€ else ๋ฌธ์˜ '์‹คํ–‰ ๋ฌธ์žฅ'์— if ๋ฌธ์ด๋‚˜ if-else ๋ฌธ์„ ๋‚ดํฌํ•  ์ˆ˜ ์žˆ๋‹ค.

//์ค‘์ฒฉ if-else ๋ฌธ ์‚ฌ๋ก€

import java.util.Scanner;
public class NestedIf {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(0~100): ");
        int score = scanner.nextInt(); //์ ์ˆ˜์ฝ๊ธฐ
        System.out.println("ํ•™๋…„์„ ์ž…๋ ฅํ•˜์„ธ์š”(1~4): ");
        int year = scanner.nextInt(); //ํ•™๋…„์ฝ๊ธฐ

        if (score >= 60){
            if (year != 4)
                System.out.println("ํ•ฉ๊ฒฉ!"); //4ํ•™๋…„ ์•„๋‹ˆ๋ฉด ํ•ฉ๊ฒฉ
            else if (score >= 70)
                System.out.println("ํ•ฉ๊ฒฉ!"); //4ํ•™๋…„์ด 70์  ์ด์ƒ์ด๋ฉด ํ•ฉ๊ฒฉ
            else
                System.out.println("๋ถˆํ•ฉ๊ฒฉ!"); //4ํ•™๋…„์ด 70์  ๋ฏธ๋งŒ์ด๋ฉด ๋ถˆํ•ฉ๊ฒฉ
        }
        else
            System.out.println("๋ถˆํ•ฉ๊ฒฉ!");

        scanner.close();
    }
}

1.(5) switch ๋ฌธ

๊ฐ’์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ๋ฐฉํ–ฅ์œผ๋กœ ๋ถ„๊ธฐํ•˜๋Š” ๊ฒฝ์šฐ, if ๋ฌธ๋ณด๋‹ค switch ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋…์„ฑ์ด ๋†’์€ ์ข‹์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. switch ๋ฌธ์€ ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋œ๋‹ค.

 char grade='B';
        switch (grade){
            case 'A':
                System.out.println("์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค.");
                System.out.println("์ž˜ํ–ˆ์Šต๋‹ˆ๋‹ค.");
                break;
            case 'B':
                System.out.println("์ข‹์•„์š”");
                break;
            case 'C':
                System.out.println("๋…ธ๋ ฅํ•˜์„ธ์š”");
                break;
            default:
                System.out.println("ํƒˆ๋ฝ์ž…๋‹ˆ๋‹ค!");
        }

switch ๋ฌธ์€ ๋จผ์ € '์‹'์„ ๊ณ„์‚ฐํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” case ๋ฌธ์œผ๋กœ ๋ถ„๊ธฐํ•œ๋‹ค. case ๋ฌธ์˜ '์‹คํ–‰ ๋ฌธ์žฅ' ์„ ์‹คํ–‰ํ•œ ํ›„ break ๋ฅผ ๋งŒ๋‚˜๋ฉด switch ๋ฌธ์„ ๋ฒ—์–ด๋‚œ๋‹ค. '์‹คํ–‰ ๋ฌธ์žฅ' ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ๋ผ๋„ ์ค‘๊ด„ํ˜ธ ({ })๋กœ ๋‘˜๋Ÿฌ์‹ธ์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜๊ธฐ ๋ฐ”๋ž€๋‹ค.

๋งŒ์ผ ์–ด๋–ค case ๋ฌธ์˜ ๊ฐ’๊ณผ๋„ ๊ฐ™์ง€ ์•Š์€ ๊ฒฝ์šฐ, default ๋ฌธ์œผ๋กœ ๋ถ„๊ธฐํ•˜์—ฌ '์‹คํ–‰ ๋ฌธ์žฅ n์„ ์‹คํ–‰ํ•œ๋‹ค. default ๋ฌธ์€ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.

  • switch ๋ฌธ์—์„œ break ๋ฌธ์˜ ์—ญํ• 
    switch ๋ฌธ์—์„œ break ๋ฌธ์€ ์ค‘์š”ํ•˜๋‹ค. ์–ด๋–ค case์˜ '์‹คํ–‰ ๋ฌธ์žฅ' ์ด ์‹คํ–‰๋˜๊ณ  ๋งŒ๋‚œ break ๋ฌธ์žฅ์€ switch ๋ฌธ์„ ๋ฒ—์–ด๋‚˜๋„๋ก ์ง€์‹œํ•œ๋‹ค. ๋งŒ์ผ case ๋ฌธ์— break ๋ฌธ์ด ์—†๋‹ค๋ฉด ์•„๋ž˜์˜ case ๋ฌธ์˜ '์‹คํ–‰ ๋ฌธ์žฅ'์œผ๋กœ break ๋ฌธ์„ ๊ณ„์† ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ๊ณ„์† ์‹คํ–‰ํ•œ๋‹ค.
  • case ๋ฌธ์˜ ๊ฐ’
    case ๋ฌธ์— ์ง€์ •ํ•˜๋Š” ๊ฐ’์€ ์ •์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด, ๋ฌธ์ž ๋ฆฌํ„ฐ๋Ÿด, ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด๋งŒ ํ—ˆ์šฉํ•œ๋‹ค. ๋‹ค์Œ ์˜ˆ๋ฅผ ๋ณด์ž.
 int b;
        switch (b%2){
            case 1: ...; break;
            case 2: ...; break; //์ •์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด 1, 2
        }
        char c;
        switch (c){
            case '+': ...; break;
            case '-': ...; break; //๋ฌธ์ž ๋ฆฌํ„ฐ๋Ÿด '+', '-'
        }
        String s = "์˜ˆ";
        switch (s){
            case "์˜ˆ": ...; break;
            case "์•„๋‹ˆ์š”": ...; break; //๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ์‚ฌ์šฉ
        }

1.(5)-(1) ์˜ˆ์ œ

import java.util.Scanner;
public class Season {
   public static void main(String[] args){
       Scanner scanner = new Scanner(System.in);

       System.out.println("์›”(1~12)์„ ์ž…๋ ฅํ•˜์‹œ์˜ค: ");
       int month = scanner.nextInt();
       switch (month){
           case 3:
           case 4:
           case 5:
               System.out.println("๋ด„์ž…๋‹ˆ๋‹ค.");
               break;
           case 6: case 7: case 8:
               System.out.println("์—ฌ๋ฆ„์ž…๋‹ˆ๋‹ค.");
               break;
           case 9: case 10: case 11:
               System.out.println("๊ฐ€์„์ž…๋‹ˆ๋‹ค.");
               break;
           case 12: case 1: case 2:
               System.out.println("๊ฒจ์šธ์ž…๋‹ˆ๋‹ค."); break;
           default:
               System.out.println("์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค.");
       }
       scanner.close();
   }
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ