【问题标题】:Why does my code throw an ArithmeticException when I expect it to simply print "Error"?为什么我的代码会在我期望它简单地打印“错误”时抛出 ArithmeticException?
【发布时间】:2015-07-15 04:09:07
【问题描述】:

我编写了一个程序,要求用户输入两个数字,如果第二个数字是 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

【问题讨论】:

  • 那么这个错误是什么时候出现的呢?当你divide(强调)?
  • 它被称为“系统”而不是“系统”
  • 你能粘贴你所有的文件吗?
  • 开括号和右括号一样多吗?
  • 是的,我相信对于每个左大括号都有一个右大括号,或者这是缺少大括号的唯一可能错误?

标签: java exception operators divide-by-zero


【解决方案1】:

你得到这个是因为当你除以 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
}

【讨论】:

  • 看到我也有,但删除了它,因为在“//handle exception here”中我尝试放置 system.out.print("error") 但我得到一个 int 错误,所以我不知道我可以在那里放什么异常
  • 感谢 caleb,感谢 try/catch 块......虽然我还没有读到它们,所以我现在想坚持基本的
  • 基本上你必须选择如果有人试图除以零会发生什么。在 Java 中,即使除法有错误,try/catch 也会让你的其余代码运行。
  • 是的,我的错误,我改变了它,但仍然是一个错误..不知道我做错了什么
【解决方案2】:

好的,我逐字复制粘贴了您的代码,将其包围在一个类中,导入 java.util.Scanner 并运行 javac。据我所知,文件末尾有两个额外的“}”。你还有其他问题:result4 和 result5 没有初始化,编译器会生你的气,因为如果 y == 0 为真,那么 dividemod 方法的返回值是未定义的。

【讨论】:

  • 明白了,谢谢,我更改了代码,但现在出现上述错误
  • 您在检查y == 0 之前正在执行result4 = x/y,当然它会引发异常。只需将result4 初始化为某个整数(可能是result4 = 0
【解决方案3】:
  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

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多