在 Java 中用 "IsInfinite" 和 "POSITIVE_INFINITY" 或 "NEGATIVE_INFINITY" 除以零

Division by zero in Java with "IsInfinite" and "POSITIVE_INFINITY" or "NEGATIVE_INFINITY"

我完全是 java 的菜鸟,我必须做到这一点:

Write a program that asks for the introduction of the dividend and the divisor (both real numbers) and show the result of the division in the output. If the result is infinite, the screen must show the text “The result is infinite”. Use the IsInfinite method for the corresponding wrapper class (or the comparison with the constants POSITIVE_INFINITY and NEGATIVE_INFINITY for the corresponding wrapper class).

主要问题是我不知道如何使用 IsInfinite 方法或常量方法(其他方法有效)。我一直在网上搜索,但没有找到解决方案。

你能帮帮我吗?

编辑:我这样做了,但我不知道这是否正是我必须做的。

import java.util.Scanner;

public class Exercise {

    public static void main(String[] args) {

        Scanner key=new Scanner(System.in);
        System.out.println("Dividend:");
        double dividend=key.nextDouble();
        System.out.println("Divisor:");
        double divisor=key.nextDouble();

        double x = dividend/divisor;
        if (x == Double.POSITIVE_INFINITY | x == Double.NEGATIVE_INFINITY) {
            System.out.println("The result is infinite");
        } else {
            System.out.println("The quotient is: " + dividend/divisor);
        }
    }
}

你提供的代码对我有用。

但是,您可能想要更改这些行:

   double x = dividend/divisor;
   if (x == Double.POSITIVE_INFINITY | x == Double.NEGATIVE_INFINITY) {

对此:

   Double x = dividend / divisor;
   if (x.isInfinite()) {

注意Double中的大写字母D。这是原语 double 的包装器 class。此 class 包含 isInfinite 方法。