简体   繁体   中英

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

I was writing code that finds the sum of the digits of the product of all n-digit numbers. I can't see the error in my code. The code is running but it is stuck in a loop. This is my code:

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 );
    }
}

If I write 1 to n Summary = 45.

In your inner ( while ) loop, you set i = i/10 , effectively making sure that i will never reach the termination condition of the for loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM