簡體   English   中英

我在我的代碼中看不到錯誤。 代碼正在運行,但卡在了一個循環中

[英]I can't see the error in my code. The code is running but it is stuck in a loop

我正在編寫代碼來查找所有 n 位數字乘積的數字總和。 我在我的代碼中看不到錯誤。 代碼正在運行,但卡在了一個循環中。 這是我的代碼:

import java.util.Scanner;
public class SummarynDigitNumbersDigitMultiplyDigit {
    public static void main (String[] args) {
        Scanner s = new Scanner(System.in);
        double n = s.nextInt();
        double Digit;
        double Summary = 0;
        double MultipliedDigit = 1;
        double start = Math.pow(10,n-1), finish=Math.pow(10,n);
        for ( double i = start; i<finish; i++){
            while (i>0) {
                Digit = i % 10;
                MultipliedDigit *= Digit;
                i = i / 10;
            }
            Summary+=MultipliedDigit;
        }
        System.out.println(Summary );
    }
}

如果我寫 1 到 n Summary = 45。

在你的內部( while )循環中,你設置i = i/10 ,有效地確保i永遠不會達到for循環的終止條件

暫無
暫無

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

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