java - 检查是否除以零,然后打印两行?

标签 java if-statement divide-by-zero infinity

我正在尝试编写一个程序,该程序将接受两个用户输入数字,然后根据它们执行计算,但我想使用 if 语句来检查它是否试图除以零或输出将是无穷大.

import java.util.Scanner;

public class work {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

double a, b, c, d;
System.out.printf("Enter the first number:");
a = scan.nextDouble();

System.out.printf("Enter the second number:");
b = scan.nextDouble();

/* various calculations  */
c = a / b;
d = b / a;


if (a > 0)
     {
System.out.printf("a/b = %.2f\n", c);
     }
if (b > 0)
{  System.out.printf("b/a = %.2f\n", d);
}

else if (a <= 0)
{  System.out.printf("a/b = %.1f\n", d);
    if (b > 0)
        System.out.printf("a/b = INF\n");
}
}
}

例如,如果我输入 4 和 5,结果会是这样的:

Enter the first number: 4
Enter the second number: 5
a/b = 0.80
b/a = 1.25

但是我无法让它检查零,并最终得到很多奇怪的输出。我怎样才能得到这样的输出?

------ Sample run 2:
Enter the first number: 0
Enter the second number: 4
a/b = 0.0
b/a = INF

------ Sample run 3:
Enter the first number: 4
Enter the second number: 0
a/b = INF
b/a = 0.0

------ Sample run 4:
Enter the first number: 0
Enter the second number: 0
a/b = INF
b/a = INF

最佳答案

您似乎遇到了多个问题。这里的第一个问题是,在决定解是否为 INF 时要检查分子。例如,如果您输入 1 和 0,您的代码会检查 a > 0(确实如此)并输出 c,即 1/0。您真正想要做的是检查方程的主项(在本例中为 b)是否等于 0。

您似乎还忘记了第一个 if 中的 else 语句,并且我不确定您在第二个 if 语句中的 else 中试图完成什么任务。

您的第三个问题是您的代码正在检查变量是否小于 0,而不是不等于 0,这会因任何负输入而产生意外结果。请记住,只有零才会导致答案未定义,或者您所说的 INF。无论如何,下面的代码应该按预期运行。请注意,我稍微修改了类名以符合 Java naming conventions .

import java.util.Scanner;

public class Work {

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    double a, b, c, d;
    System.out.print("Enter the first number: ");
    a = scan.nextDouble();

    System.out.print("Enter the second number: ");
    b = scan.nextDouble();

    /* various calculations  */
    c = a / b;
    d = b / a;


    if (b != 0)
    {
      System.out.printf("a/b = %.2f\n", c); /* the dominator (b) is not
                                             zero, so the solution is a/b
                                             (stored in the variable c) */
    }
    else
    {
      System.out.print("a/b = INF\n"); /* the dominator (b) is zero, 
                                        so the solution is INF */
    }

    if (a != 0)
    {  
      System.out.print("b/a = %.2f\n", d); /* the dominator (a) is not
                                            zero, so the solution is a/b
                                            (stored in the variable d) */
    }
    else
    {  
      System.out.printf("b/a = INF\n"); /* the dominator (a) is zero, 
                                         so the solution is INF */
    }
  }
}

关于java - 检查是否除以零,然后打印两行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35421486/

相关文章:

java - 无法在 glassfish 3.1 中将字符集从 ISO-8859-1 更改为 UTF-8

java - 如果在循环内没有按预期工作java

javascript - 您如何使else语句什么都不做?

r - 强制除以零默认为 NaN 而不是 Inf

c++ - 除以零预防 : Checking the divisor's expression doesn't result in zero vs. 检查除数不为零?

python - 在 numpy(子)数组中除以零

java - LinkedBinaryTree<E> 类的 toString() 方法

java - 在数据库中存储和搜索集(具有许多可能的值)(来自 Java)

java - 什么是 JAVA 执行()?

r if else 基于多个条件