Java 因式分解失败 - 为什么会循环?

Java Factorization Failure - Why is it looping?

我正在尝试获取代码来查找用户输入的因数,为用户输入给出质因数分解,并给出 lcm 和 gcm。它应该很简单。这是编程入门 class,但对我来说它进展得太快了。我花了几个小时阅读和研究并试图让这段代码工作。请帮忙

import java.util.Scanner;


public class PattersonFactorization {


    public static void main(String[] args) 
    {
        //create scanner to obtain input from command window
        Scanner input = new Scanner(System.in);

        //initialization phase
        int input1 = 0; //initialize first input from user
        int input2 = 0; //initialize second input from user


        //prompt twice for two inputs from user 
        //until they enter a positive value for each input

        do
        {
            System.out.println("Please provide your first positive number: ");
            input1 = input.nextInt();
        }//end do for input1
        while(input1 <= 0);

        do
        {
            System.out.println("Please provide your next positive number: ");
            input2 = input.nextInt();
        }//end do for input2
        while(input2 <= 0);

        // call methods for factorization calculations
        calculateFactors(input1);
        calculateFactors(input2);
        calculatePrime(input1);
        calculatePrime(input2);
        calculateLCM(input1, input2);
        calculateGCF(input1, input2);


    }//end of main

    public static void calculateFactors(int input)
    {

        for(int countFactor = 1; countFactor < input; countFactor++)
        {
            if(countFactor % input == 0); 
            System.out.println(countFactor);
        }

    }//end of calculateFactors

    public static void calculatePrime(int input)
    {
        for(int countPrime = 1; countPrime < input; countPrime++)
        {
            if(countPrime % input == 0);
            System.out.println(countPrime);
            input = input/countPrime;
            countPrime--;
        }
    }//end of calculatePrime

    public static void calculateLCM(int input1, int input2)
    {
        for(int factorNum = 1; input1 % factorNum != 0 && input2 % factorNum != 0; factorNum++)
        System.out.println(factorNum);
    }//end of calculateLCM

    public static void calculateGCF(int input1, int input2)
    {
        for(int factorNum = input1; input1 % factorNum != 0 && input2 % factorNum != 0; factorNum--)
        System.out.println(factorNum);
    }

}//end of class

`

我认为问题出在这里:

        for(int countPrime = 1; countPrime < input; countPrime++)
        {
            if(countPrime % input == 0);
            System.out.println(countPrime);
            input = input/countPrime;
            countPrime--;
        }

递减 countPrime递增 的循环中。结果它永远不会改变它的价值。

您的代码存在一些问题:

if(countFactor % input == 0);
System.out.println(countFactor);

无论您的条件表达式的值如何,此处的代码将始终执行 System.out.println(countFactor);。问题是 if(countFactor % input == 0); - 您的 if 末尾有一个分号。

您可以使用 no braces in this case

if(countFactor % input == 0)
    System.out.println(countFactor);

但是使用 大括号 几乎总是更清楚地向编码器和编译器显示范围:

if(countFactor % input == 0) {
    System.out.println(countFactor);
}

您的无限循环来自 for 循环中的递增和递减 countPrime

for(int countPrime = 1; countPrime < input; countPrime++)
{
    if(countPrime % input == 0);
    System.out.println(countPrime);
    input = input/countPrime;
    countPrime--;
}

countPrime 将在循环结束时递减为 0,然后在下一次循环迭代时递增回 1input = input/countPrime; 将始终是 input = input/1,这是终止表达式永远不会是 true.

的另一个原因