JAVA

자바 절댓값 구하기

정숙씨의 쏠쏠한 코딩 2021. 2. 18. 13:31

java 프로그램이 언어를 통한 절대값을 구현할수있다.

-Math.abs 함수 사용-

public class Math{

   public static void main(String[] args){

          int x = -11;

          System.out.println("절댓값 : " + Math.abs(x));

    }

}

결과값 : 11

-Math.abs 함수 사용 하지 않을시 -

public class Math{

    public static void main (String[] args){

     int x  = -11;

     if(x<0)

         {

              x = -x;

              System.out.println("절댓값 : " + x);

         }

    }

결과값 : 11

-부호를 통하여 절대값을 구현할수있다.