簡體   English   中英

使用 Typecaste (Java) 除以零異常

[英]Divide by Zero Exception with Typecaste (Java)

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: / 為零

參考第 25 行(我的 for 循環條件)。

循環工作正常,直到我嘗試將 digit 的值鍵入為 int,而且我不確定為什么這會導致循環的任何部分除以零。 我已經討論了有關使用有理表達式的條件的所有可能性,並且無法推斷出任何分母都將設置為零的偶然性。 無論輸入什么數字,我都會收到此錯誤。 如果不是因為我的教授提供了一個數字,該數字的值超過了他的一個測試用例中可以存儲在 int 中的值,我根本不會選擇將數字保存為雙精度數。 該程序在類型轉換之前運行良好,並為所有其他測試用例提供了正確的答案。

它使用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,然后用於除法和崩潰。

如果要更正問題,則需要更改算法,或者使用更精確的內容。

你在這做

(number * 10) /i !=0

數字是兩倍。

由於存儲方式(以尾數和指數形式),因此不建議使用浮點數進行比較。 所以,如果這個條件返回true,請認為自己很幸運。

因為你的循環有點永無止境的循環。 你獲得算術異常的原因是,在這種無限循環中你將i乘以10。 “i”達到32位的“整數”限制並溢出並最終使所有這32位為0。

實際上,i = 0並且以下兩個拋出除以零的異常

(number * 10) /i 

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

第一次控制進入循環時, 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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM