Java Trinomial Operation

Record·2021년 3월 24일
0

오늘의 개념 정리 [Trinomial Operation]

What's trinomial operation?

  • 3개의 요소로 이뤄짐
  • 조건식 ? (참일 때의 값) : (거짓일 때의 값)의 형태로 사용
  • 조건식의 계산 결과
    참인 경우 ':' 왼쪽의 값 또는 식으로 변경
    거짓인 경우 ':' 오른쪽의 값 또는 식으로 변경
  • 복잡한 계산식이나 조건 처리, 비교 구조를 간단히 표현 가능하게 함
  • 중첩해 이용 시, 여러 값들을 순서대로 비교해 가장 큰/ 작은 값 계산 가능

How to use

import java.util.Scanner;

public class 파일 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 정수 2개 입력
        int a = sc.nextInt(), b = sc.nextInt();
        System.out.println(a > b ? a : b); // 참 = a 출력
                                           // 거짓 = b 출력
        // 정수 3개 입력
        int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
        System.out.println((a > b ? a : b) > c ? (a > b ? a : b) : c); // a, b, c 중 가장 큰 값 출력
        System.out.println((a < b ? a : b) < c ? (a < b ? a : b) : c); // a, b, c 중 가장 작은 값 출력
    }
}
profile
👨🏻‍💻 Web Developer

0개의 댓글