Chapter 04. 제어문(1)

박철민·2021년 7월 30일
0
post-thumbnail

수강 날짜 21-07-30

강의명 : 01. if문-1
강의명 : 02. if문-2
강의명 : 03. switch-case문

if문

if문, if-else문
조건식의 결과에 따라 수행문이 실행되는 조건문
if문 형식

	if(조건문){
    		수행문;
        }

	if(조건문){
    		수행문1;
        }
        else{
        	수행문2;
        }

예제

public class IfExample1 {
	public static void main(String[] args) {
		
		char gender = 'F';
		
		if(gender == 'F') {
			System.out.println("여성입니다");
			
		}
	}
}

if else 예제

package ifexample;

public class IfExample1 {
	public static void main(String[] args) {
		
		char gender = 'M';
		
		if(gender == 'F') {
			System.out.println("여성입니다");	
		} 
		else {
			System.out.println("여성이 아닙니다.");
		}
	}
}

하나의 조건에 대한 여러 경우에 대해 수행문이 각각 다른 경우

if - else if - else를 통해 각 조건에 맞는 경우를 수행할 수 있게 해준다.

예제
각 나이별로 요금을 계산하여 나이와 요금을 출력하라

package ifexample;

public class IfExample1 {
	public static void main(String[] args) {
		
		int age = 10;
		
		int charge = 0;
		
		if( age < 8) {
			charge = 1000;
		} 
		else if( age < 14) {
			charge = 1500;
		}
		else if( age < 20) {
			charge = 2000;
		}
		else {
			charge = 3000;
		}

		System.out.println("나이 : " + age);
		System.out.println("요금 : " + charge);
		
	}
}

Scanner : 입력을 받게 해주는 특별한 클래스

		Scanner scanner = new Scanner(System.in);
		
		int age = scanner.nextInt();

예제

package ifexample;

import java.util.Scanner;

public class IfExample1 {
	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		int score = scanner.nextInt();
		
		char grade;
		
		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("점수 : " + score);
		System.out.println("학점 : " + grade);
		
	}
}


**조건문과 조건 연산자```

public class IfExample1 {
	public static void main(String[] args) {
		
		int a = 10;
		int b = 20;
		
		int max;
		
		max = (a > b)? a: b;
		System.out.println(max);
	}
}

switch-case문

조건이 정수, 문자열 값으로 그 값에 ㄸ라 수행 결과가 달라지는 경우
if-else if-else와 같은 의미로 수행

switch(변수) {
	case 조건1: 수행문1
    	break;
	case 조건2: 수행문2
    	break;
	default : 수행문3
		break;
}

예제

public class IfExample3 {
	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		int rank = scanner.nextInt();
		char medalColor;
		
		switch(rank) {
			case 1: medalColor = 'G';
				System.out.println("금메달");
				break;
			case 2: medalColor = 'S';
				System.out.println("은메달");
				break;
			case 3: medalColor = 'B';
				System.out.println("동메달");
				break;
			default : medalColor = 'A';
		}
		
		System.out.println(rank + "등은 " + medalColor + "메달 입니다.");
	}
}

break문 : 문장에서 즉시 나온다.
switch case에서는 break를 적어줘여 밑에것이 수행되지 않는다.

초기화를 하지 않는 변수는 오류를 발생할 수 있다,

Switch Case문에서 자바7에서는 문자열 사용 가능




switch-case문 문제
각 월에 따른 한달 날짜 수를 day 변수 값으로 출력하세요
단 2월은 28일로 합니다.

public class IfExample3 {
	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);
		
		int month = scanner.nextInt();
		int day = 0;
		
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = 28;
			break;
		default:
			day = 30;
			break;
		}
		System.out.println(month + "월은 " + day + "일입니다");
		
	}
}

profile
취준좀비 컴공

0개의 댓글

관련 채용 정보