你的程序我没看懂,求因数应该是非常简单的啊。如果是要求素因数那就要复杂一些了。特别是求一个很大的半素数的素因数。 import java.util.Scanner;public class DivisionFactor { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.printf("Enter a integer: "); int i = input.nextInt(); for (int j = 2; j <= i / 2; j++) { if (i % j == 0) { System.out.println(j); } } } }
import java.util.Scanner;public class DivisionFactor { public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.printf("Enter a integer: ");
int i = input.nextInt();
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
System.out.println(j);
}
}
}
}
我觉得不需要变回false吧,因为打印数字前一定有一个都好
跳出while循环以后flag就没关系了因为while里面是2个if语句,因此应该是每个if都会执行吧。?
我去再改一改
修改后这样是正确的
import java.util.Scanner;
public class DivisionFactor
{ public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
System.out.printf("Enter a integer: ");
int i = input.nextInt();
int factor=2;
boolean flag=false;
while(i!=1)
{
if(0 == i%factor)
{
if(flag)
{
System.out.printf(", ");
}
System.out.printf("%d",factor);
i/=factor;
factor=2;
flag=true;
}
else
{
factor++;
}
}
System.out.printf("."); }}