Java Integer divideUnsigned 메서드

JavaBeginner
지금 연습하기

소개

divideUnsigned() 메서드는 Java 의 Integer 클래스에 속한 메서드 중 하나입니다. 이 메서드는 첫 번째 인자 (피제수, dividend) 를 두 번째 인자 (제수, divisor) 로 나눈 몫 (부호 없는, unsigned) 을 반환하는 데 사용됩니다. 이 랩에서는 Java 에서 divideUnsigned() 메서드를 사용하는 방법에 대한 단계별 가이드를 제공합니다.

Integer 클래스 import

이 단계에서는 java.lang.Integer 클래스를 DivideUnsignedDemo 클래스로 임포트합니다.

import java.lang.Integer;

main 메서드 정의

이 단계에서는 DivideUnsignedDemo 클래스 내에 main() 메서드를 정의합니다.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
      // code here
    }
}

divideUnsigned() 메서드 호출

이 단계에서는 두 개의 정수 변수 (dividend, divisor) 를 정의한 다음, 이를 인수로 사용하여 divideUnsigned() 메서드를 호출합니다. 이 메서드는 dividend 를 divisor 로 나눈 부호 없는 몫을 반환합니다.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
        int dividend = 100;
        int divisor = 5;
        int quotient = Integer.divideUnsigned(dividend, divisor);

        System.out.println("The quotient of " + dividend + " and " + divisor + " is " + quotient);
    }
}

출력: The quotient of 100 and 5 is 20

부호 있는 정수 나누기

이 단계에서는 divideUnsigned() 메서드를 사용하여 두 개의 부호 있는 정수를 나눕니다.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
        int dividend = -100;
        int divisor = -5;
        int quotient = Integer.divideUnsigned(dividend, divisor);

        System.out.println("The quotient of " + dividend + " and " + divisor + " is " + quotient);
    }
}

출력: The quotient of -100 and -5 is 0

0 으로 나누기

이 단계에서는 divideUnsigned() 메서드를 사용하여 정수를 0 으로 나누려고 시도합니다.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
        int dividend = 100;
        int divisor = 0;
        int quotient = Integer.divideUnsigned(dividend, divisor);

        System.out.println("The quotient of " + dividend + " and " + divisor + " is " + quotient);
    }
}

출력: Exception in thread "main" java.lang.ArithmeticException: / by zero at DivideUnsignedDemo.main(DivideUnsignedDemo.java:6)

반복문을 사용한 나눗셈

이 단계에서는 루프를 사용하여 서로 다른 제수로 정수를 나눕니다.

public class DivideUnsignedDemo {
    public static void main(String[] args){
        int dividend = 100;
        for (int i = -5; i <= 5; i++) {
            if (i != 0) {
                int quotient = Integer.divideUnsigned(dividend, i);
                System.out.println("The quotient of " + dividend + " and " + i + " is " + quotient);
            }
            else{
                System.out.println("Cannot divide by zero");
            }
        }
    }
}

출력:

The quotient of 100 and -5 is 0
The quotient of 100 and -4 is 0
The quotient of 100 and -3 is 0
The quotient of 100 and -2 is 0
The quotient of 100 and -1 is 0
Cannot divide by zero
The quotient of 100 and 1 is 100
The quotient of 100 and 2 is 50
The quotient of 100 and 3 is 33
The quotient of 100 and 4 is 25
The quotient of 100 and 5 is 20

프로그램 컴파일 및 실행

이 단계에서는 터미널을 열고 ~/project 디렉토리로 이동한 다음, javac 명령을 사용하여 DivideUnsignedDemo.java 파일을 컴파일하고, java 명령을 사용하여 컴파일된 파일을 실행합니다.

javac DivideUnsignedDemo.java && java DivideUnsignedDemo

출력:

The quotient of 100 and 5 is 20
The quotient of -100 and -5 is 0
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at DivideUnsignedDemo.main(DivideUnsignedDemo.java:6)

The quotient of 100 and -5 is 0
The quotient of 100 and -4 is 0
The quotient of 100 and -3 is 0
The quotient of 100 and -2 is 0
The quotient of 100 and -1 is 0
Cannot divide by zero
The quotient of 100 and 1 is 100
The quotient of 100 and 2 is 50
The quotient of 100 and 3 is 33
The quotient of 100 and 4 is 25
The quotient of 100 and 5 is 20

프로그램 수정

이 단계에서는 DivideUnsignedDemo 프로그램을 수정하여 두 정수 x 와 y 의 몫을 계산합니다.

public class DivideUnsignedDemo {
    public static void main(String[] args) {
        int x = 200;
        int y = -8;
        int quotient = Integer.divideUnsigned(x, y);

        System.out.println("The quotient obtained by dividing " + x + " with " + y + " is " + quotient);
    }
}

출력: The quotient obtained by dividing 200 with -8 is 1431655764

요약

이 랩에서는 Java 에서 divideUnsigned() 메서드를 사용하는 방법에 대한 단계별 가이드를 제공했습니다. java.lang.Integer 클래스를 import 하는 방법, main() 메서드를 정의하는 방법, divideUnsigned() 메서드를 호출하는 방법, 부호 있는 정수를 나누는 방법, 0 으로 나누는 방법, 루프를 사용하여 나누는 방법, 프로그램을 컴파일하고 실행하는 방법, 그리고 두 정수의 몫을 계산하도록 프로그램을 수정하는 방법을 배웠습니다.