繁体   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