: if, if ~ else, if ~ else if ~ else, switch ~ case
조건에 따라 수행하거나 수행하지 않는 코드 블럭 생성
if
if (조건) {수행 코드}
: (조건)이 참(true)이면, {수행 코드}를 실행 / 거짓(false)이면, {수행 코드}를 실행하지 않음
제한 없이 중첩 가능. (if 내부에 if)
if ~ else
if (조건) {수행 코드 1} else {수행 코드 2}
: (조건)이 참(true)이면, {수행 코드 1}을 실행 / 거짓(false)이면, {수행 코드 2}를 실행
if ~ else if ~ else
if (조건 1) {수행 코드 1} else if (조건 2) {수행 코드 2} else {수행 코드 3}
: (조건 1)이 참(true)이면, {수행 코드 1}을 실행 / (조건 1)이 거짓(false)이면서 (조건 2)가 참(true)이면, {수행 코드 2}를 실행 / (조건 1)과 (조건 2) 둘 다 거짓(false)이면 {수행 코드 3}을 실행
else if는 제한 없이 추가 가능. (if, else는 한 번만 가능)
◎ 이 경우 앞선 조건이 참(true)이어서 조건에 해당하는 수행 코드를 실행한 이후에는 그 이후 조건 및 수행 코드는 실행하지 않는다. (◆Example 2)
◆Example 1
package ch03;
import java.util.Scanner;
public class Ch03Ex24 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 1. 가위 2. 바위 3. 보 입력
// 컴퓨터 값 생성(random)
// 입력값과 생성값 비교(if), 결과 출력
System.out.println("1. 가위 2. 바위 3. 보");
System.out.print("선택 : ");
int uChoice = sc.nextInt();
int comChoice = (int) (Math.random() * 3 + 1);
sc.close();
System.out.println("선택값 : " + uChoice);
System.out.println("컴퓨터 : " + comChoice);
if (uChoice == comChoice) {
System.out.println("무승부");
} else if (uChoice == 1) {
if (comChoice == 2) {
System.out.println("패배");
} else {
System.out.println("승리");
}
} else if (uChoice == 2) {
if (comChoice == 1) {
System.out.println("승리");
} else {
System.out.println("패배");
}
} else if (uChoice == 3) {
if (comChoice == 1) {
System.out.println("패배");
} else {
System.out.println("승리");
}
} else {
System.out.println("입력값이 올바르지 않습니다.");
}
}
}
출력 결과
1. 가위 2. 바위 3. 보
선택 : 3
선택값 : 3
컴퓨터 : 2
승리
◆Example 2
package ch03;
import java.util.Scanner;
public class Ch03Ex26 {
public static void main(String[] args) {
// 국, 영, 수 점수(0~100) 입력
// 평균값 도출 > 학점 계산 (90이상 A, 80이상 B, 70이상 C, 60이상 D, 그 이하 F)
Scanner sc = new Scanner(System.in);
int kor, eng, math, sum;
double avg;
System.out.print("국어 점수 : ");
kor = sc.nextInt();
System.out.print("영어 점수 : ");
eng = sc.nextInt();
System.out.print("수학 점수 : ");
math = sc.nextInt();
sc.close();
sum = kor + eng + math;
avg = sum / 3.0;
System.out.println("점수 합계 : " + sum);
System.out.println("평균 점수 : " + avg);
if (avg < 0 || avg > 100) {
System.out.println("입력값이 올바르지 않습니다.");
} else if (avg >= 90 && avg <= 100) {
System.out.println("학점 : A");
} else if (avg >=80) {
System.out.println("학점 : B");
} else if (avg >= 70) {
System.out.println("학점 : C");
} else if (avg >= 60) {
System.out.println("학점 : D");
} else {
System.out.println("학점 : F");
}
}
}
출력 결과
국어 점수 : 73
영어 점수 : 98
수학 점수 : 85
점수 합계 : 256
평균 점수 : 85.33333333333333
학점 : B
switch ~ case
switch (A) {case B: 수행 코드 1; default: 수행 코드 2}
: (A == B)가 참(true)이면, {case B : 수행 코드 1}을 실행 / 거짓(false)이면 {default: 수행 코드 2}를 실행
A에 올 수 있는 값은 byte, short, int, long, char, String만 가능
◎ 문제점
(A == B)가 참(true)이면, 해당하는 case B: 이하의 모든 수행 코드를 실행
해결 방법 : 각각의 case : 수행 코드; 뒤에 break;를 추가
결과 : (A == B)가 참(true)인, case B: 이하의 수행 코드만 실행
package ch03;
import java.util.Scanner;
public class Ch03Ex31 {
public static void main(String[] args) {
// 계산기
Scanner sc = new Scanner(System.in);
int fNum, sNum, result = 0;
String cal;
System.out.print("정수 입력 : ");
fNum = sc.nextInt();
System.out.print("기호 입력 (+, -, *, /) : ");
cal = sc.next();
if (!(cal.equals("+") || cal.equals("-") || cal.equals("*") || cal.equals("/"))) {
System.out.println("입력값이 올바르지 않습니다.");
System.out.println("프로그램을 종료합니다.");
sc.close();
System.exit(0); //시스템 종료
}
System.out.print("정수 입력 : ");
sNum = sc.nextInt();
sc.close();
System.out.println("입력 수식 : " + fNum + " " + cal + " " + sNum);
switch (cal) {
case "+":
result = fNum + sNum;
break;
case "-":
result = fNum - sNum;
break;
case "*":
result = fNum * sNum;
break;
default:
result = fNum / sNum;
break;
}
System.out.println("계산 결과 : " + result);
}
}
출력 결과
정수 입력 : 156
기호 입력 (+, -, *, /) : +
정수 입력 : 13
입력 수식 : 156 + 13
계산 결과 : 169