【问题标题】:Getting Arithmetic Exception to handle multiplying by zero获取算术异常来处理乘以零
【发布时间】:2012-11-26 20:30:45
【问题描述】:

对于家庭作业,我必须创建一个简单的程序,将两个数字作为参数,然后将它们相乘。如果其中一个数字为零,则程序抛出 ArithmeticException

当我阅读文档时,我总是觉得 AritmeticException 只处理除以零错误和其他数学不可能的事情。但是,赋值希望这个内置处理程序完成工作,那么如何让它接受乘以零作为错误呢?

到目前为止我的代码(它被编码为只处理除以零和其他“标准”数学错误)

public class MultTwo {

public static void main(String[] args) {

    try {
        int firstNum = Integer.parseInt(args[0]);
        int secondNum = Integer.parseInt(args[1]);
        System.out.println(firstNum*secondNum);
    }
    catch (ArithmeticException a) {
        System.out.println("You're multplying by zero!");
    }

}//end main
}//end MultTwo Class

【问题讨论】:

  • 如果任何值为零,则抛出 ArithmeticException

标签: java exception math


【解决方案1】:

怎么样

if (firstNum == 0 || secondNum == 0) {
   throw new ArithmeticException("You're multplying by zero!");
}

虽然这不是一个好习惯,但我猜你的老师想用它给你看一些东西。

【讨论】:

  • 基本上就是这样。然后我们继续创建一个自定义的异常类来为我们做这件事。现在我看到她正试图向我们展示投掷的工作原理。
  • 是的,所以我认为这取得了成功。
【解决方案2】:

如你所见,这种情况下不会自动抛出异常,所以你需要自己抛出:

if (firstNum == 0 || secondNum == 0) {
    throw new ArithmeticException("Numbers can't be null");
}
//continue with the rest of your code.

注意事项:

  • 你不需要抓ArithmeticException
  • 你应该抓住NumberFormatException,以防输入不是有效的整数。

【讨论】:

    【解决方案3】:

    我知道您应该抛出异常,而不是处理它。比如:

    int multiply(int firstNum, int secondNum)
    {
        if(firstNum == 0 || secondNum == 0)
            throw new ArithmeticException("Multplying by zero!");
        return firstNum * secondNum;
    }
    

    【讨论】:

      【解决方案4】:

      jvm 永远不会抛出 ArithmeticException 用于将任何数字与零相乘,您必须明确地抛出它。

      你可以这样做:

      try {
          int firstNum = Integer.parseInt(args[0]);
          int secondNum = Integer.parseInt(args[1]);
          if(firstNum == 0 || secondNum == 0){
                throw new ArithmeticException();
          }
          else{
                System.out.println(firstNum*secondNum);
          }
      }
      catch (ArithmeticException a) {
          System.out.println("You're multplying by zero!");
      }
      

      【讨论】:

        【解决方案5】:

        算术系统永远不会抛出乘以零的异常,因为这是完全有效的,例如

        double res = 3.141 * 0.0;
        

        给出 0.0。

        相反,您需要检测一个零,如果有则抛出,例如

        if (res == 0.0) {
           throw new ArithmeticException("You have a zero");
        }
        

        您可以检查任一输入是否为零。您可以改为检查结果是否为零,因为(理论上)如果一个或两个输入为零,您只能获得零输出。然而,Java 不能以无穷小精度存储数字,两个非常小的输入可能会产生零。例如

        Double.MIN_NORMAL * Double.MIN_NORMAL
        

        给我0.0

        上述部分不适用于Integers,因此您可以在那种情况下检查您的结果。

        【讨论】:

          【解决方案6】:

          试试这个........

          import java.util.Scanner;
          
          
          public class T {
          
              public static void main(String[] args) {
          
          
                      int firstNum = new Scanner(System.in).nextInt();
                      int secondNum = new Scanner(System.in).nextInt();
          
                      if (firstNum == 0 || secondNum == 0) {
          
                          throw new ArithmeticException("Numbers can't be zero");
                      }
          
                      else{
                      System.out.println(firstNum*secondNum);
                      }
          
          
          
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 2018-04-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-16
            • 1970-01-01
            • 2012-09-01
            • 1970-01-01
            相关资源
            最近更新 更多