public static String factorX(long x){
String factor="";
long number = x;
long i = 2;
while (i < number) {
if (number % i == 0) {
factor += i+", ";
number /= i;
} else {
i++;
}
}
return factor;
For example if I put 120, i get 2,2,2,3. I am missing factor 5. Everything else works find;
就是那个没有显示的东西....................................... ..............................
答案 0 :(得分:3)
变化:
while (i < number) {
为:
while (i <= number) {
你很高兴。但是,这不会分别打印1或2作为数字1和2的主要因素。所以你可能想要添加这样的东西:
if(x < 2) {
return "" + x;
}
return factor;