자바 연산자 조건 연산자 ?
조건연산자
형식 : (조건) ? (참 일때의 수식) : (거짓 일때의 수식)
if 문등을 몇줄에 걸쳐 써야 하는것을 간략하게 기술 할 수 있도록 해준다.
다음 수식의 결과는?
int i=10;
int result = i<5 ? i+10 : i/2;
System.out.println(result);
public class ConditionOperation {
public static void main(String[] args) {
int nHats=1;
System.out.println("I Have "+nHats + " hat" + (nHats==1?".":"s."));
public static void main(String[] args) {
int nHats=1;
System.out.println("I Have "+nHats + " hat" + (nHats==1?".":"s."));
nHats++;
System.out.println("I Have "+nHats + " hat" + (nHats==1?".":"s."));
}
}
System.out.println("I Have "+nHats + " hat" + (nHats==1?".":"s."));
}
}
댓글 없음:
댓글 쓰기