Java Integer divideUnsigned 方法

JavaJavaBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

divideUnsigned() 方法是 Java 中 Integer 类的一个方法。该方法用于返回第一个参数(被除数)除以第二个参数(除数)所得到的无符号商。本实验将逐步指导你如何在 Java 中使用 divideUnsigned() 方法。

导入 Integer 类

在这一步中,将 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() 方法。该方法返回被除数除以除数得到的无符号商。

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

除以零

在这一步中,尝试使用 divideUnsigned() 方法将一个整数除以零。

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 类、定义 main() 方法、调用 divideUnsigned() 方法、对带符号整数进行除法运算、除以零、使用循环进行除法运算、编译并运行程序,以及修改程序以计算两个整数的商。

您可能感兴趣的其他 Java 教程