자체임상실험
Inform Restaurant
자체임상실험
전체 방문자
오늘
어제
  • 분류 전체보기 (89)
    • IT정보와 지식 TABLE (1)
      • IT정보 및 꿀팁 (0)
      • Windows (1)
      • Linux (0)
    • Linux TABLE (0)
      • 취약점 점검 스크립트 (0)
    • Developer TABLE (33)
      • Java (33)
      • Java Algorithm (0)
    • DataBase TABLE (1)
      • SQLD-P (1)
    • Cloud TALBE (0)
      • AWS (0)
    • Security TABLE (1)
      • 비박스(bee-box) 취약점 점검 (1)
      • 보안기사 (0)
    • Writer TABLE (42)
      • 기사필사 (42)
      • 서평 (0)
    • Growth TABLE (10)
      • 국민취업지원제도 (4)
      • 국비학원 (6)
    • Life TABLE (0)
      • 자서전 (0)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 배열
  • Switch
  • 초장기 주택담보대출
  • @
  • if
  • 논설위원
  • 디엠제트
  • 방사능
  • 국비학원
  • 조건문
  • while
  • 논설위원필사
  • 경향신문
  • Arrays
  • 한겨레
  • 국민취업지원제도
  • Java
  • 동아일보
  • 조용한 고용
  • 해양오염수
  • 반복문
  • do while
  • for
  • 문자열비교
  • KH정보교육원
  • 배열복사
  • 지하공간
  • 천자칼럼
  • array
  • 2차원배열

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
자체임상실험

Inform Restaurant

Developer TABLE/Java

삼항 연산자

2021. 8. 25. 17:16

삼항 연산자

  • 조건식의 결과 값에 따라 연산을 처리하는 연산자
  • 조건식이 참일 경우 식1을 실행, 거짓일 경우 식2를 실행
  • 삼항 연산자 중첩 사용 가능

짝수, 홀수 확인

public void method1() {
	int nInput = 0;
	String sResult = "";
	
	Scanner s = new Scanner(System.in);
	System.out.printf("Input Number(e.g. 3): ");
	nInput = s.nextInt();
	
	sResult = (nInput % 2 == 0) ? "EVEN" : "ODD";
	System.out.printf("%d is \"%S\"\n", nInput, sResult);
}

결과값

Input Number(e.g. 3): 100
100 is "EVEN"

양수, 음수, 0 확인

public void method() {
	int nInput = 0;
	String sResult = "";
	
	Scanner s = new Scanner(System.in);
	System.out.println("Input Integer: ");
	nInput = s.nextInt();
	
	sResult = (nInput > 0) ? "Positive" : (nInput < 0) ? "Negative" : "Zero";
	
	System.out.printf("Result: " + sResult);
}

결과값

Input Integer: 10
Result: Positive

Input Integer: -7
Result: Negative

Input Integer: 0
Result: Zero

3개 정수를 입력받아 비교하여 모두 같으면 true, 아니면 false를 출력

public void practice6() {
	int nData1 = 0;
	int nData2 = 0;
	int nData3 = 0;
	boolean bResult = false;
	
	Scanner s = new Scanner(System.in);
	System.out.printf("Insert Integer1: ");
	nData1 = s.nextInt();
	System.out.printf("Insert Integer2: ");
	nData2 = s.nextInt();
	System.out.printf("Insert Integer3: ");
	nData3 = s.nextInt();
	
	bResult = (nData1 == nData2) && (nData2 == nData3);
	
	System.out.println("Result: " + bResult);
}

결과값

Insert Integer1: 10
Insert Integer2: -3
Insert Integer3: 7
Result: false

Insert Integer1: 7
Insert Integer2: 7
Insert Integer3: 7
Result: true

삼항 연산자로 계산기 만들기

public void method2() {
	int nData1 = 0;
	int nData2 = 0;
	char cOp = ' ';
	String sResult = "";
	
	Scanner s = new Scanner(System.in);
	System.out.printf("Input Integer1: ");
	nData1 = s.nextInt();
	
	System.out.printf("Input Integer2: ");
	nData2 = s.nextInt();
	s.nextLine();		// 버퍼에 입력되어 있는 개행문자 제거
	
	System.out.printf("Input Operator: ");
	cOp = s.nextLine().charAt(0);
	
	
	sResult = (cOp == '+') ? (nData1 + nData2) + "" :	// 빈문자열을 결합하여 연산된 결과값을 문자열로 변환
				(cOp == '-') ? (nData1 - nData2) + "" :
				(cOp == '*') ? (nData1 * nData2) + "" :
				(cOp == '/') ? (nData1 / nData2) + "" :
				"\"ERROR\"";

	System.out.printf("%d %c %d = %s\n", nData1, cOp, nData2, sResult);
}

결과값

Input Integer1: 92
Input Integer2: 67
Input Operator: *
92 * 67 = 6164

Input Integer1: 28
Input Integer2: 35
Input Operator: a
28 a 35 = "ERROR"
저작자표시 비영리 (새창열림)

'Developer TABLE > Java' 카테고리의 다른 글

조건문(제어문)  (0) 2021.08.25
복합 대입 연산자  (0) 2021.08.25
논리 연산자, Short-circuit evaluation  (0) 2021.08.25
    'Developer TABLE/Java' 카테고리의 다른 글
    • 조건문 if (if ~ else , if ~ else if ~ else)
    • 조건문(제어문)
    • 복합 대입 연산자
    • 논리 연산자, Short-circuit evaluation
    자체임상실험
    자체임상실험
    생활에 유용한 정보와 일상을 담은 휴식처, Sobremesa 입니다.

    티스토리툴바