【问题标题】:Check if dividing by zero, then printing two lines?检查是否除以零,然后打印两行?
【发布时间】:2016-02-16 00:12:57
【问题描述】:

我正在尝试编写一个程序,该程序将接受两个用户输入的数字,然后根据它们执行计算,但我想使用 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

【问题讨论】:

  • 你得到了什么奇怪的输出?
  • 您正在执行数学运算,然后检查它是否为&lt;= 0。首先检查它不是0 并且只是iff它没有执行除法。

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


【解决方案1】:

看来您这里有多个问题。您的第一个问题是您在决定解决方案是否为 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 */
    }
  }
}

【讨论】:

  • 啊,我太着急了,我犯了很多错误..非常感谢。
【解决方案2】:

我注意到您正试图从控制台获取一个整数作为双精度数。这可能会导致各种奇怪的输出。 所以把你的代码改成

a = scan.nextInt();
 b = scan.nextInt();

这应该可以正常工作。谢谢你

【讨论】:

  • 谢谢,但我仍然无法弄清楚我需要如何告诉它何时打印 INF 或零。
  • 你应该在计算前检查零,而不是计算后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多