JAVA
삼항 연산자
jiyoon12
2025. 4. 23. 00:01
- 조건식의 결과가 참(true)인 경우와 거짓(false)인 경우에 따라 다른 결과가 수행된다.
- if(조건문)을 간단히 표현할 때 사용할 수 있다.
package ch03;
public class Operation9 {
// 메인 함수 (코드의 진입점)
public static void main(String[] args) {
int number = 7; //테스트 할 숫자
// 1. 삼항 연산자로 홀/짝 판단
// String --> 문자열 --> "안녕 반가워"
String result = (number % 2 == 0) ? "짝수" : "홀수";
System.out.println("result : " + result);
boolean isOk = (5 > 3) ? true : false;
System.out.println("isOk : " + isOk);
// 두 수 중에 큰 수를 max 라면 변수에 담고 출력 하시오.
int max = (10 > 1) ? 10 : 1;
System.out.println("max : " + max);
} // end of main
} // end of class
package ch03;
public class Operation10 {
// 메인 함수 (코드의 진입점)
public static void main(String[] args) {
int n1 = 100;
int n2 = 500;
int max;
// F
// 100 > 500
max = (n1 > n2) ? n1 : n2;
System.out.println("max : " + max);
} // end of main
} // end of class