• 산술 연산자: +, -, *, /, %(나머지 값)
  • 증감 연산자: 1을 더하거나 빼는 연산자로 위치에 따라 결과값이 다르게 나타남
    -전위 연산: (++3, --2) 연산자가 앞쪽에 배치 --> 다른 연산자보다 먼저 증/감
    -후위 연산: (3++, 2--) 연산자가 뒤쪽에 배치 --> 다른 연산자보다 나중에 증/감
  • 비교 연산자: 데이터가 같은지, 다른지 비교할 때 쓰이며 결과값은 항상 논리값(T/F)로 나타남
    -<, >, <=, >=, ==, !=
    -등호는 항상 오른쪽에 위치
  • 논리 연산자: 논리값 두 개 비교하는 연산자(&&(And), ||(Or)
    -&&(And): 다 true이면 true, 그 외의 경우에는 false (~와, ~이고, ~ 이면서, ~부터 ~까지, ~사이)
    -||(Or): 둘 다 false이면 false, 나머지는 true (AND의 반대) (~또는, ~거나)
  • 논리 부정 연산자: ! -> 논리 값을 반대로 바꾸는 연산자
  • 복합 대입 연산자: +=, -=, *=, /=, %=
    ->피연산자가 자신과 연산 후 결과를 다시 자신에게 대입
  • 삼항 연산자: 조건식 ? 식1 : 식2
    조건식의 결과가 true이면 식1, 결과가 false이면 식2를 수행하는 연산자
    *조건식: 연산 결과가 true/false인 식 (비교, 논리, 논리부정 연산 포함)

1. 코드 실행용 클래스

package edu.kh.op.ex;

public class ExampleRun { 
	
	public static void main(String[] args) {
		
		//OpExample 생성
		OpExample ex = new OpExample();
		
		ex.ex1(); //ex가 가지고 있는 ex1() 메소드 실행
		/* 출력값
		 OpExample 클래스에 ex1()기능 수행
		 클레스 분리 테스트
	     println 자동완성
		 */
		
		ex.ex2();
		/* 출력값
         정수 입력1: 5
		 정수 입력2: 3
		 5 / 3 = 1
		 5 % 3 = 2
		*/
		
		ex.ex3();
		/* 출력값
		 iNum1: 11
		 iNum2: 9
		 11
		 temp1: 6
		 5
		 temp2: 2
		 4 / 4 / 7
		 */
		
		ex.ex4();
		/* 출력값
		 false
		 true
		 true
		 false
		 true
		 ------------------------------------------
		 true
		 true
		 false
		 -------------------------------------------
		 temp는 짝수인가? false
		 temp는 짝수인가? false
		 temp는 3의 배수인가? true
		 temp는 4의 배수인가? false
		 temp는 5의 배수인가? false
		 */
         
         ex.ex5();
		/*
		 100이상인가? :true
		 짝수인가?: true
		 조건을 만족하는가? true
		 1이상?: true
		 10이하?: true
		 조건 만족?: true
		 --------------------------------------------
		 true
		 */
		
		ex.ex6();
		/*
		bool1: true
		bool2: false
		----------------------------------
		정수 입력: 55
		55은/는 1 이상, 100 이하의 정수인가?: true

		 */
		
		ex.ex7();
		/*
		a를 1 증가: 2
		a를 4 증가: 6
		a를 10 감소: -4
		a를 3배 증가: -12
		a를 6으로 나눴을 때 몫: -2
		a를 2로 나눴을 때 나머지: 0
		 */
		
		ex.ex8();
		/*
		num은 30 이하의 수이다.
		----------------------------------
		정수 입력: -50
		음수입니다.
		 */
		
	}

}

2. 예제 코드 전용 클래스

package edu.kh.op.ex;

import java.util.Scanner;

public class OpExample {  
	
	//ex1() 메소드 -> OpExample이 가지고 있는 기능 중 ex1()이라는 기능
	public void ex1() {
		System.out.println("OpExample 클래스에 ex1()기능 수행"); 
		System.out.println("클레스 분리 테스트"); 
		System.out.println("println 자동완성"); 
	}
    
	//ex2() 메소드
	public void ex2() {
		Scanner sc = new Scanner(System.in);
		System.out.print("정수 입력1: ");
		int input1 = sc.nextInt(); //다음 입력되는 정수 읽어옴
		
		System.out.print("정수 입력2: ");
		int input2 = sc.nextInt(); 
		
		System.out.printf("%d / %d = %d\n", input1,input2, (input1/input2));
		System.out.printf("%d %% %d = %d\n", input1,input2, (input1%input2));
		//                    %하나만 붙이면 오류나기 때문에 두 개(%%) 붙임
		
	}
    
	public void ex3() { 
		//증감 연산자 (++, --) --> 피연산자(값)를 1 증가/감소 시키는 연산자
		
		int iNum1=10;
		int iNum2=10;
		
		iNum1++; //1증가
		iNum2--; //1감소
		
		System.out.println("iNum1: "+ iNum1); //11
		System.out.println("iNum2: "+ iNum2); //9
		
		//전위 연산
		int temp1=5;
		System.out.println(++temp1+5); //11
						// ++5    +5
						// +6     +5 ==11
		System.out.println("temp1: "+temp1); //6
		
		//후위 연산
		int temp2=3;
		System.out.println(temp2-- +2); //5
						// 3--     +2 ==5
						// temp2= 2;
		System.out.println("temp2: "+temp2); //2
		
		int a = 3;
		int b = 5;
		int c = a++ + --b; 
		System.out.printf("%d / %d / %d\n",a ,b, c); //4/4/7
	}
    
	public void ex4( ) {
		//비교 연산자
        
		int a =10;
		int b = 20;
		System.out.println(a>b); //false
		System.out.println(a<b); //true
		System.out.println(a!=b); //true
		System.out.println(a==b); //false
		System.out.println((a==b) ==false); //true
		
		System.out.println("------------------------------------------");
		int c = 4;
		int d = 5;
		System.out.println( c<d ); //true
		System.out.println(c+1 <= d ); //true
		System.out.println((++c != d) == ( --c != d));  //false
		                // (++4 != 5) -> false
						//               (--5 != 5) -> true
						// false == true  --> false
		
		System.out.println("-------------------------------------------");
		int temp = 723;
		System.out.println("temp는 짝수인가? " + (temp % 2 ==0)); //false
		System.out.println("temp는 짝수인가? " + (temp % 2 !=1)); //false
		System.out.println("temp는 3의 배수인가? " + (temp %3 ==0)); //true
		System.out.println("temp는 4의 배수인가? " + (temp %4 ==0)); //false
		System.out.println("temp는 5의 배수인가? " + (temp %5 ==0)); //false
	}
    public void ex5() {
		//논리 연산자: &&(AND), || (OR)
		
        //&&
		int a = 100;
		//a는 100 이상이면서 짝수인가?
		System.out.println("100이상인가? :" + (a>=100) );//true
		System.out.println("짝수인가?: "+ (a % 2 ==0));//true
		System.out.println("조건을 만족하는가? "+((a>=100) && (a % 2 ==0)));//true
		
		int b =5;
		//b는 1부터 10까지 숫자 범위에 포함되어 있는가
		//b는 1부터 10 사이의 숫자인가
		System.out.println("1이상?: "+(b>=1)); //true
		System.out.println("10이하?: "+(b<=10)); //true
		System.out.println("조건 만족?: "+((b>=1)&&(b<=10))); //true
		
		System.out.println("--------------------------------------------");
		
		// || (OR) 연산자
		int c =10;
		//c는 10을 초과했거나 짝수인가?
		System.out.println((c>10)||(c%2==0)); //true
		
	}
	public void ex6() {
		//논리 부정 연산자: !
		
		boolean bool1 = true;
		boolean bool2 = !bool1; //false
		
		System.out.println("bool1: "+bool1); //true
		System.out.println("bool2: "+bool2); //false
		
		System.out.println("----------------------------------");
		
		Scanner sc = new Scanner(System.in);
		
		//정수를 하나 입력받았을 때 
		//1) 해당 정수가 1부터 100사이 값 맞는지 확인 (1이상 100이하)
		//2) 1부터 100사이 값이 아닌지 확인 (1의 반대)
		
		System.out.print("정수 입력: ");
		int input = sc.nextInt();
		
		//1<=input <=100
		boolean result1= (input>=1 && input<=100);
		System.out.printf("%d은/는 1 이상, 100 이하의 정수인가?: %b\n", input, result1);
		
		//1미만 또는 100초과
		boolean result2 = (input<1 || input>100);
		boolean result3 = !(input>=1 && input<=100);
		System.out.printf("%d은/는 1 미만, 100 초과인가?: %b %b\n", input,result2,result3);
	}
	public void ex7() {
		
		//복합 대입 연산자: +=, -=, *=, /=, %=
		
		int a=1;
		//a를 1 증가
		a++;// a= a+1, a+=1
		System.out.println("a를 1 증가: " +a); //11
		
		//a를 4 증가
		a += 4;
		System.out.println("a를 4 증가: "+ a); //15
		
		//a를 10 감소
		a -= 10;
		System.out.println("a를 10 감소: "+ a); //5
		
		//a를 3배 증가
		a *= 3;
		System.out.println("a를 3배 증가: "+ a); //15
		
		//a를 6으로 나눴을 때 몫
		a /= 6;
		System.out.println("a를 6으로 나눴을 때 몫: "+ a); //2
		
		//a를 2로 나눴을 때 나머지
		a %= 2;
		System.out.println("a를 2로 나눴을 때 나머지: "+ a); //0
		
		
	}
	
	public void ex8() {
		
		//삼항 연산자: 조건식 ? 식1 : 식2
		
		int num=30;
		//num이 30보다 크면: "num은 30보다 큰 수이다."
		//else          : "num"은 30 이하의 수이다."
		String str1 = "num은 30보다 큰 수이다.";
		String str2 = "num은 30 이하의 수이다.";
		
		String result = num>30 ? str1 : str2 ;
					  //  조건식 ? 식1  : 식2
					  //          true  false
		// num이 30 초과하면 str1, 아니면 str2 --> result 변수에 저장
		
		System.out.println(result); //num은 30 이하의 수이다.
		
		System.out.println("----------------------------------");
		
		//입력 받은 정수가 음수인지 양수인지 구분 (단 0은 양수)
		
		//ex)
		//정수 입력: 4
		//양수입니다.
		
		//정수 입력: -5
		//음수입니다.
		
		Scanner sc = new Scanner(System.in);
		System.out.print("정수 입력: ");
		int input = sc.nextInt();
		
		String str3="양수입니다.";
		String str4="음수입니다.";
		String result1 = input>=0 ? str3 : str4 ;
		
		System.out.println(result1);
		// System.out.println((String result1 = input>=0 ? "양수" : "음수")+"입니다." ) ;
		//코드 간단하게
		
	}


}
profile
개발 일지

0개의 댓글