반응형
사용방법은 "조건식 ? 값 또는 연산자 : 값 또는 연산자" 입니다.
즉 저 3항 연산자를 if문으로 풀어 쓰면 아래 코드와 같습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(조건식) { | |
값 또는 연산식 | |
} else { | |
값 또는 연산식 | |
} |
예제를 써서 간단하게 써보겠습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int score = 80; | |
char grade = (score > 90) ? 'A' : 'B'; // 이항연산자를 grade 변수에 대입 | |
System.out.println("당신의 점수는 " + grade + "입니다."); // 삼항연산자 조건에 맞는 글자 출력하라! |
이로써 변수에 대입이 가능해졌습니다.
grade 변수를 이용해서 다양하게 사용이 가능합니다.
반응형
'공부 > 개발노트' 카테고리의 다른 글
[이것이 자바다] if문과 ifelse문 변형 (0) | 2018.10.28 |
---|---|
[이것이 자바다] 문제 3번 답안 (0) | 2018.10.28 |
jQuery 함수 선언 방법 (0) | 2018.04.09 |
업힙(UpHeap) 알고리즘 정의하기 (0) | 2016.09.29 |
다운힙(DownHeap) 알고리즘 정의하기 (0) | 2016.09.29 |