当我希望它只是打印“错误”时,为什么我的代码会抛出ArithmeticException?

时间:2015-07-15 04:09:08

标签: java exception operators divide-by-zero

我已经制作了一个程序,要求用户输入两个数字,如果第二个数字是0,它应该给出一个错误。但是,我收到一个错误,如下所示。我有一个if-else语句,但它并没有按照我的预期进行。我不确定我做错了什么。

public static void main(String[] args) {
    int x, y;
    Scanner kbd = new Scanner(System.in);

    System.out.print("Enter a: ");
    x = kbd.nextInt();
    System.out.print("Enter b: ");
    y = kbd.nextInt();

    int result = add(x, y);
    int result2 = sub(x, y);
    int result3 = multi(x, y);
    int result4 = divide(x, y);
    int result5 = mod(x, y);

    System.out.println(x + " + " + y + " = " + result);
    System.out.println(x + " - " + y + " = " + result2);
    System.out.println(x + " * " + y + " = " + result3);
    System.out.println(x + " / " + y + " = " + result4);
    System.out.print(x + " % " + y + " = " + result5);
}

public static int add(int x, int y) {
    int result;
    result = x + y;
    return result;
}

public static int sub(int x, int y) {
    int result2;
    result2 = x - y;
    return result2;
}

public static int multi(int x, int y) {
    int result3;
    result3 = x * y;
    return result3;
}

public static int divide(int x, int y) {
    int result4;
    result4 = x / y;
    if (y == 0) {
        System.out.print("Error");
    } else {
        result4 = x / y; 
    }
    return result4; 
}

public static int mod(int x, int y) {
    int result5;
    result5 = x % y;
    if (y == 0) {
        System.out.print("Error");
    } else {
        result5 = x % y;
    }
    return result5;
}

输出 我收到这个错误..

Enter a: 10
Enter b: 0
Exception in thread "main" java.lang.ArithmeticException: / by zero

2 个答案:

答案 0 :(得分:3)

你得到这个,因为当你除以0时,Java会抛出异常。如果您只想使用if语句来处理它,那么请使用以下内容:

public static int divide(int x, int y){
   int result;
   if ( y == 0 ) {
 // handle your Exception here
 } else {
   result = x/y; 
  }
  return result; 
}

Java还通过try / catch块处理异常,它在try块中运行代码,并将处理catch块中异常的处理方式。所以你可以这样做:

try {  
       result4 = divide(a, b);
}
catch(//the exception types you want to catch ){
     // how you choose to handle it
}

答案 1 :(得分:0)

  public static void main(String[] args) {

   int x,y;
   Scanner kbd = new Scanner(System.in);

   System.out.print("Enter a: ");
   x = kbd.nextInt();
   System.out.print("Enter b: ");
   y = kbd.nextInt();


   int result = add(x,y);
   int result2 = sub(x,y);
   int result3 = multi(x,y);
   int result4 = divide(x,y);
   int result5 = mod(x,y);

   System.out.println(x +" + "+ y +" = "+ result);
   System.out.println(x +" - "+ y +" = "+ result2);
   System.out.println(x +" * "+ y +" = "+ result3);
   System.out.println(x +" / "+ y +" = "+ result4);
   System.out.print(x +" % "+ y +" = "+ result5);

   }

  public static int add(int x, int y){
   int result;
   result = x+y;
     return result;
     }


  public static int sub(int x, int y){
   int result2;
   result2 = x-y;
     return result2;
     }

  public static int multi(int x, int y){
       int result3;
       result3 = x * y;
          return result3;
     }

   public static int divide(int x, int y){
       int result4;
       result4 = 0;
        if ( y == 0 ) {
        System.out.print("Error");
          } 
        else{
          result4 = x/y; 
          }
         return result4; 
     }

   public static int mod(int x, int y){
      int result5;
      result5 = 0;
         if ( y == 0) {
            System.out.println("Error!");
          }
          else{
            result5 = x % y;
            }
            return result5;

}
}      

<强>输出

Enter a: 4
Enter b: 0
ErrorError!
4 + 0 = 4
4 - 0 = 4
4 * 0 = 0
4 / 0 = 0
4 % 0 = 0