使用Typecaste(Java)除以零异常

时间:2015-09-23 04:28:34

标签: java casting dividebyzeroexception

import java.util.Scanner;


public class SumDigits {

public static void main(String[] args)
{
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);

    // prompt the user to enter a value and store it as the integer called number
    System.out.print("Enter an integer: ");
    double number = Math.abs(input.nextDouble());

    System.out.println("The sum of the digits is " + sumNumbers(number));

    input.close();
}
public static int sumNumbers (double number)
{

    int sum = 0;

    for (int i = 10, digit = 0; (number * 10) /i !=0; i *= 10, digit = (int)((number % i) - digit)/(i / 10))
    {
        sum += digit;
    }

    return sum;
}
}

在运行时,我收到错误消息

  

线程中的异常" main" java.lang.ArithmeticException:/ by zero

参考第25行(我的循环条件)。

循环工作正常,直到我尝试将数字的数字类型转换为int,并且我不确定为什么会导致循环的任何部分将某些部分除以零。我已经完成了关于使用理性表达式的条件的所有可能性,并且不能推导出任何分母将被设置为零的意外事件。无论输入什么数字,我都会收到此错误。如果不是因为我的教授提供的数字超过可以存储在他的一个测试用例中的int中的数字,我就不会选择将数字保存为双数。该程序在类型转换之前运行良好,并为所有其他测试用例提供了正确的答案。

4 个答案:

答案 0 :(得分:2)

它导致int而不是double失败的原因是因为该值溢出。如果您在每一步显示i的值(从10开始),您可以看到以下值:

10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
1410065408
1215752192
-727379968
1316134912
276447232
-1530494976
1874919424
1569325056
-1486618624
-1981284352
1661992960
-559939584
-1304428544
-159383552
-1593835520
1241513984
-469762048
-402653184
268435456
-1610612736
1073741824
-2147483648

然后失败了。它使用的最后一个值-2147483648是可以存储在int中的最小值。当你在下一次循环中将它乘以10时,它变为0,然后用于除法和崩溃。

如果您想纠正这个问题,您需要更改算法,或者使用更精确的内容。

答案 1 :(得分:1)

你在这里做

(number * 10) /i !=0

其中数字是双倍的。

由于存储方式(以尾数和指数形式),建议不要使用浮点数进行比较。所以,如果这个条件返回true,请认为自己很幸运。

因为你的循环有点永无止境的循环。你获得算术异常的原因是,在这种无限循环中你将i乘以10。 " I"到达"整数"限制为32位并溢出,最终使所有32位为0。

实际上,i = 0并且以下两个抛出除以零异常

(number * 10) /i 

(number % i) - digit)/(i / 10)

答案 2 :(得分:0)

第一次控制进入循环时,digit的值将为零,而不是您预期的输入数字的最后一位数。此外,您需要修复循环终止条件,因为这是错误的。例如,如果数字是123,那么最后预期的迭代1230/1000 = 1而不是0并且循环不会结束而是导致溢出。您可以使用以下内容:

public static int sumNumbers (double number)
    {
        int sum = 0;
        for (int i = 10, digit = (int)(number % i); digit != 0; i *= 10, digit = (int)((number % i) - digit)/(i / 10))
        {
            sum += digit;
        }

        return sum;
    }

这应该可行,但我再次建议您改进此代码,因为这不是理想的方法。

<强>测试

Enter an integer: 3456 The sum of the digits is 18

答案 3 :(得分:0)

ArithmeticException的根本原因是错误的循环条件。在循环中添加System.out.println("i=" + i + " i*10=" + (i * 10));会产生输出:

i=10 i*10=100
i=100 i*10=1000
i=1000 i*10=10000
i=10000 i*10=100000
i=100000 i*10=1000000
i=1000000 i*10=10000000
i=10000000 i*10=100000000
i=100000000 i*10=1000000000
i=1000000000 i*10=1410065408
i=1410065408 i*10=1215752192
i=1215752192 i*10=-727379968
i=-727379968 i*10=1316134912
i=1316134912 i*10=276447232
i=276447232 i*10=-1530494976
i=-1530494976 i*10=1874919424
i=1874919424 i*10=1569325056
i=1569325056 i*10=-1486618624
i=-1486618624 i*10=-1981284352
i=-1981284352 i*10=1661992960
i=1661992960 i*10=-559939584
i=-559939584 i*10=-1304428544
i=-1304428544 i*10=-159383552
i=-159383552 i*10=-1593835520
i=-1593835520 i*10=1241513984
i=1241513984 i*10=-469762048
i=-469762048 i*10=-402653184
i=-402653184 i*10=268435456
i=268435456 i*10=-1610612736
i=-1610612736 i*10=1073741824
i=1073741824 i*10=-2147483648
i=-2147483648 i*10=0