我需要将输出因子作为素数。
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a number:");
int theNum = keyboard.nextInt();
int i;
System.out.println("\nThe prime factors of " + theNum + " are:");
for(i=1; i <= theNum/2; i++)
if(theNum % i == 0)
{
System.out.print(i + " ");
}
}
}
答案 0 :(得分:0)
你基本上只需要检查素数
我从here复制的一些素数检查函数:
boolean isPrime(int n) {
//check if n is a multiple of 2
if (n%2==0) return false;
//if not, then just check the odds
for(int i=3;i*i<=n;i+=2) {
if(n%i==0)
return false;
}
return true;
}
然后只需添加isPrime(i)
支票:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a number:");
int theNum = keyboard.nextInt();
System.out.println("\nThe prime factors of " + theNum + " are:");
for(int i = 1; i <= theNum / 2; i++)
if (theNum % i == 0 && isPrime(i))
System.out.print(i + " ");
}