JAVA/JAVA BASIC

[JAVA] 조건문

아잠만_ 2024. 3. 8. 12:03

조건문

if 문

if ( 조건식 ) {
     실행문;
     실행문;
     ....
}

실행문이 1개일 시 중괄호 생략 가능

public void method1() {
	// 점수를 입력 받아 60점 이상이면 합격
	// 미만이면 불합격을 출력
		
	int score = sc.nextInt();
	if (score >= 60) {
		System.out.println("합격");
	}
	if (score < 60) {
		System.out.println("불합격");
	}
}

else if / else

public void method3() {
	boolean b = false;
	boolean c = true;
	if (b) {
		System.out.println("참");
	} else if (c) {
		System.out.println("b가 거짓이면서 c가 참");
	} else {
		System.out.println("나머지 경우");
	}
}
활용 예시
public void method2() {
	//90점 이상이면 A
	//80점 이상이면 B
	//70점 이상이면 C
	//60점 이상이면 D
	//60점 미만이면 F
	int score = sc.nextInt();
	if (score >= 90) {
		System.out.println("A");
	} 
	if (score >= 80 && score < 90) {
		System.out.println("B");
	} 
	if (score >= 70 && score < 80) {
		System.out.println("C");
	} 
	if (score >= 60 && score < 70) {
		System.out.println("D");
		} 
	if (score < 60) {
		System.out.println("F");
	} 
}
public void method4() {
	//method2 else if와 else 활용하여 풀이
	int score = sc.nextInt();
	if (score >= 90) {
		System.out.println("A");
	} else if (score >= 80) {
		System.out.println("B");
	} else	if (score >= 70) {
		System.out.println("C");
	} else	if (score >= 60) {
		System.out.println("D");
	} else {
		System.out.println("F");
	} 
}
public void method6() {
	// A~F 학점 표기
	// + : 7~9, 0 : 4~6, - : 0~3
	// ex ) 97 A+
	//		82 B-
	//		50 F
	int score = sc.nextInt();
	if (score >= 90) {
		System.out.print("A");
		if (score%10 >= 7) {
			System.out.println("+");
		} else if (score%10 >= 4) {
			System.out.println("0");
		} else {
			System.out.println("-");
		}
	} else if (score >= 80) {
		System.out.print("B");
		if (score%10 >= 7) {
			System.out.println("+");
		} else if (score%10 >= 4) {
			System.out.println("0");
		} else {
			System.out.println("-");
		}
	} else	if (score >= 70) {
		System.out.print("C");
		if (score%10 >= 7) {
			System.out.println("+");
		} else if (score%10 >= 4) {
			System.out.println("0");
		} else {
			System.out.println("-");
		}
	} else	if (score >= 60) {
		System.out.print("D");
		if (score%10 >= 7) {
			System.out.println("+");
		} else if (score%10 >= 4) {
			System.out.println("0");
		} else {
			System.out.println("-");
		}
	} else {
		System.out.println("F");
}

 

switch문

switch(변수) {
   case 값1 :             //변수 == 값1

   실행문A
   break;

   case 값2 :              //변수 == 값2
   실행문A
   break;

   default :                  //변수 != 값1, 값2
   실행문C
}
public void method1() {
	int a = 10;
	switch (a) {
	case 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("그 외 값");
		break;
	}
}
if(a==1) System.out.println("1");
if(a==2) System.out.println("2");
if(a==3) System.out.println("3");
if(a==4) System.out.println("4");
if(a==5) System.out.println("5");
else System.out.println("그 외 값");
public void method3() {
	// 달을 입력하고 끝자리를 출력하시오
	// 1, 3, 5, 7, 8, 10 ,12 -> 31
	// 4, 6, 9, 11 -> 30
	// 2 -> 28, 29
	
	int month = sc.nextInt();
	switch (month) {
	case 1: case 3: case 5: case 7: case 8 : case 10 : case 12 : 
		System.out.println("31");
		break;
	case 4 : case 6: case 9: case 11 :
		System.out.println("30");
		break;
	case 2 :
		System.out.println("28, 29");
		break;
	default:
		System.out.println("잘못 입력되었습니다.");
		break;
	}
}
public void method5() {
	// 학점 나타내기
	// A~F
	int score = sc.nextInt();
	switch (score/10) {
	case 10 :
		System.out.println("A");
		break;
	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");
		break;
	}
}

switch문은 부등호 수식을 사용할 수 없음