본문 바로가기
Programming Languages/Java

Chapter 8. 조건

by 더 이프 2023. 1. 9.
728x90

목차

    1. 조건

    ■ 조건문 if ~ else

    • 제어 흐름
    • 가정적 조건을 나타내며 만약 ~라면을 의미
    • else는 if에서 제안한 조건이 아닐 경우에 실행
    • else if를 사용시 여러가지 조건을 넣을 수 있음

    ■ 조건문 switch

    • 해당 조건에 맞는 case를 실행시키는 것
    • case의 실행 후 아래로 case가 남아 있을 경우 해당 case부터 밑의 case를 순서대로 그대로 실행
    • case 실행 후 다른 case에서 실행되지 않도록 break를 사용
    • 제어문 실행 중에 break이후의 명령을 실행하지 않고 빠져나옴
    • default는 만족하는 case가 없을 경우 실행(else와 같은 역할)

     

    2. 예시

    ■ Conditional1

    package j06_조건;
    
    public class Conditional1 {
    
    	public static void main(String[] args) {
    
    		int num = 10;
    		int num2 = 9;
    
    		if (num > num2) {
    			System.out.println("num이 num2보다 큽니다.");
    			System.out.println("num : " + num);
    		} else if (num < num2) {
    			System.out.println("num이 num2보다 작습니다");
    		} else {
    			System.out.println("num이 num2가 같습니다.");
    		}
    	}
    }

    ■ Conditional2

    package j06_조건;
    
    import java.util.Scanner;
    
    public class Conditional2 {
    
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    
    		// 일렬로 나열하여 사용 가능하나 클린 코드가 아님
    //		int a, b, c; 
    //		a = b = c = 0;
    
    		int a = 0;
    		int b = 0;
    		int c = 0;
            
    		int max = 0;
    		int min = 0;
    
    		System.out.println("정수 3개 입력 : ");
    		a = scanner.nextInt();
    		b = scanner.nextInt();
    		c = scanner.nextInt();
    
    		max = a;
    		min = a;
    
    		if (b > max)
    			max = b;
    		if (c > max)
    			max = c;
    		if (b < min)
    			min = b;
    		if (c < min)
    			min = c;
    
    		System.out.println("최대값 : " + max);
    		System.out.println("최소값 : " + min);
    	}
    }

    ■ Conditional3

    package j06_조건;
    
    import java.util.Scanner;
    
    public class Conditional3 {
        /*
         * 조건
         * score가 0점보다 작거나 100점보다 크면 계산불가
         * score가 90 ~ 100점이면 A학점 
         * score가 80 ~ 89점이면 B학점
         * score가 70 ~ 79점이면 C학점
         * score가 60 ~ 69점이면 D학점
         * score가 0 ~ 59점이면 F학점
         * 
         * + 학점 추가
         */
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    
    		int score = 0;
    		String grade = null;
    
    		System.out.print("점수 입력 : ");
    		score = scanner.nextInt();
    
    		// grade 값 설정
    		if (score < 0 || score > 100) {
    			grade = null;
    		} else if (score > 89) {
    			grade = "A";
    		} else if (score > 79) {
    			grade = "B";
    		} else if (score > 69) {
    			grade = "C";
    		} else if (score > 59) {
    			grade = "D";
    		} else {
    			grade = "F";
    		}
            
    		// + 설정
    		if ((score > 59 && score < 101) && (score % 10 > 4 || score == 100)) {
    			grade += "+";
    		}
            
    		// 출력부
    		if (grade == null) {
    			System.out.println("계산 불가");
    		} else {
    			System.out.println("점수(" + score + ") : " + grade + "학점");
    		}
    	}
    }

    ■ Switch1

    package j06_조건;
    
    public class Switch1 {
    
    	public static void main(String[] args) {
    
    		String select = "C선택";
    
    		switch (select) {
    		case "A선택":
    			System.out.println("PC(A)를 연결합니다.");
    			break;
    		case "B선택":
    			System.out.println("PC(B)를 연결합니다.");
    			break;
    		case "C선택":
    			System.out.println("PC(C)를 연결합니다.");
    			break;
    		case "D선택":
    			System.out.println("PC(D)를 연결합니다.");
    			break;
    		default:
    			System.out.println("else와 같은 역할");
    		}
    	}
    }

    ■ Switch2

    package j06_조건;
    
    import java.util.Scanner;
    
    public class Switch2 {
    
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    
    		int score = 0;
    
    		System.out.println("점수 입력 : ");
    		score = scanner.nextInt();
    
    		if (score < 0 || score > 100) {
    			score = 0;
    		}
    
    		switch (score / 10) {
    		case 10:
    		case 9:
    			System.out.println("A학점");
    			break;
    		case 8:
    			System.out.println("B학점");
    			break;
    		case 7:
    			System.out.println("C학점");
    			break;
    		case 6:
    			System.out.println("D학점");
    			break;
    		default:
    			System.out.println("F학점");
    		}
    	}
    }