관리 메뉴

커리까지

자바 기초 프로그래밍 강좌 4강 - 연산자 본문

자바

자바 기초 프로그래밍 강좌 4강 - 연산자

목표는 커리 2021. 2. 2. 08:57
728x90
SMALL
  • 정수를 나타내는 자료형이 많은 이유는 자료형이 차지하는 메모리 공간의 크기가 다르기때문이다.

연산자

  • 계산의 기본
  • 사칙연산이나 나머지

시분초 받아서 출력하기

public class Main {
    final static int SECOND = 1000;

    public static void main(String[] args){

        int minute = SECOND / 60;
        int second = SECOND % 60;

        System.out.println(minute + "분" + second + "초");
    }
}

>
16분 40초    

증감 연산자

public class Main {

    public static void main(String[] args){

        int a = 10;

        System.out.println("현재의 a는" + a + "입니다.");
        a++;
        System.out.println("현재의 a는" + a + "입니다.");
        System.out.println("현재의 a는" + ++a + "입니다.");
        System.out.println("현재의 a는" + a++ + "입니다.");
        System.out.println("현재의 a는" + a + "입니다.");
    }
}

>
현재의 a는 10입니다.
현재의 a는 11입니다.
현재의 a는 12입니다.
현재의 a는 12입니다.
현재의 a는 13입니다.
  • ++이 앞에 붙으면 출력되기전에 연산이 실행되고 뒤에 붙어있으면 출력된 이후에 연산이 수행된다.

%연산자

public class Main {

    public static void main(String[] args){


        System.out.println(1 % 3);
        System.out.println(2 % 3);
        System.out.println(3 % 3);
        System.out.println(4 % 3);
        System.out.println(5 % 3);
        System.out.println(6 % 3);


    }
}

>
1    
2
0
1
2
0   

==, > , <, &&, ||, ! 알아보기

public class Main {

    public static void main(String[] args){

        int a = 50;
        int b = 50;

        System.out.println("a와 b가 같은가요?" + (a == b));
        System.out.println("a가 b보다 큰가요?" + (a > b));
        System.out.println("a가 b보다 작은가요?" + (a < b));
        System.out.println("a가 b와 같으면서 a가 30보다 큰가요?" + ((a == b) && (a > 30)));
        System.out.println("a가 50이 아닌가요?" + !(a == 50));

    }
}

>
a와 b가 같은가요? true
a가 b보다 큰가요? false    
a가 b보다 작은가요? false    
a가 b와 같으면서 a가 30보다 큰가요? false
a가 50이 아닌가요? false    

조건, 참,거짓

public class Main {

    public static void main(String[] args){

        int x = 50;
        int y = 60;


        System.out.println(max(a,y));

    }
    // 반환형, 함수이름, 매개 변수
    static int max(int a, int b){
        int result = (a > b) ? a: b;
        // a가 b보다 크다면 a를 넣고 b가 더 크가면 b를 넣는다. true : fasle
        return result;
    }
}

>
60    

pow() 제곱

public class Main {

    public static void main(String[] args){

        dobule a = Math.pow(3.0,20.0);

        System.out.println((int) a);

    }

}
728x90
LIST
Comments