我在编程课上完成这个因子生成器时遇到了一些麻烦。它应该取一个数字,并使用 nextFactor 方法打印出所有因子。当我将数字设置为假设 150 时,它会打印出“1 2 3 5”,它应该打印“2 3 5 5”。那么,我应该从这里去哪里?我看过Java - Factor Generator program nextfactor method,但它并没有引起我的任何查询
public class FactorGenerator
{
//user inputs int from scanner in FactorTester class
public FactorGenerator(int i)
{
num = i;
}
//Checks to see if num can be factored, but does not factor it.
//Goes through all possible factors of num and returns true if the remainder == 0
public boolean hasMoreFactors()
{
for(int i = 1; i < num; i++)
{
//check if the remainder is anything other then 0
if(num % i == 0)
{
return true;
}
}
return false;
}
//Actually factors num and prints out the factor at the end of every loop.
public void nextFactor()
{
for(int i = 1; i < num; i++)
{
//check if the remainder is anything other then 0
if(num % i == 0)
{
System.out.println(i);
num /= i;
}
}
System.out.println("Done.");
}
private int num;
}