所以我已经编写了一个简单的程序,返回用户通过scanner类输入的数字的因子。但我需要这个程序只打印该数字的素数因子,但我不知道如何改变我的代码,以便能够做到这一点。谢谢您!
主要类别:
package factoring_integers;
import java.util.Scanner;
public class Factoring_Integers {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer: ");
int num = in.nextInt();
FactorGenerator myFacGenerator = new FactorGenerator(num);
while (myFacGenerator.hasMoreFactors()){
System.out.println(myFacGenerator.nextFactor());
}
}
}
二等舱:
public class FactorGenerator {
public FactorGenerator(int a) {
number = a;
factor = 2;
}
public int nextFactor() {
return factor++;
}
private final int number;
private int factor;
private boolean done;
public boolean hasMoreFactors() {
boolean hasMoreFactors = false;
done = false;
if (factor < number) {
while (!done) {
if (number % factor != 0) {
factor++;
}
else {
done = true;
hasMoreFactors = true;
}
}
}
return hasMoreFactors;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!