Java Display the Prime Factorization of a number

前端 未结 7 1572
闹比i
闹比i 2020-11-30 13:02

So for my assignment, I have to write a program that asks the user for an integer input and then print out that number\'s prime factorization. This is what I have:



        
7条回答
  •  庸人自扰
    2020-11-30 13:24

    You are almost there! Move the if-continue block outside the for loop. Otherwise, it "continues" the inner-most loop, rather than the one you intended.

    while (number % i == 0) {
        number /= i;
        count++;
    }
    if (count == 0) {
        continue;
    }
    System.out.println(i+ "**" + count);
    

    Alternatively, you could enclose the System.out.println call in if (count != 0), because it's the only statement following the continue:

    while (number % i == 0) {
        number /= i;
        count++;
    }
    if (count != 0) {
        System.out.println(i+ "**" + count);
    }
    

    Your program on ideone: link.

提交回复
热议问题