2012-09-24 30 views
6

因此,對於我的任務,我必須編寫一個程序,要求用戶輸入一個整數,然後打印出該數字的素因子分解。 這是我有:Java顯示數字的素數分解

import java.util.Scanner; 

public class PrimeFactor { 
    public static void main(String[] args) { 
     System.out.print("Enter a positive number: "); 
     Scanner scanner = new Scanner (System.in); 
     int number = scanner.nextInt(); 
     int count; 
     for (int i = 2; i<=(number); i++) { 
      count = 0; 
      while (number % i == 0) { 
       number /= i; 
       count++; 
       if (count == 0) { 
        continue; 
       } 
      } 
      System.out.println(i+ "**" + count); 
     } 
    } 
} 

我現在的問題是,每當我一樣,數量15453運行它,我從1各因素的列表,以100和它的指數,當我只想要素質因素,而我堅持如何繼續。

+0

你可以使用遞歸,函數,庫類嗎?只需要弄清楚你在這裏可以使用什麼。 – thatidiotguy

+3

'繼續'聲明不會經常執行... – Keppil

回答

0

你接近:

  1. 的的System.out.println語句必須是內部的for循環中,只顯示爲count>0
  2. 取出if(count == 0) { continue; },這是無用的,因爲你剛剛增加count
4

你快到了!將if-continue塊移到for循環之外。否則,它會「繼續」最內層的循環,而不是你想要的循環。

while (number % i == 0) { 
    number /= i; 
    count++; 
} 
if (count == 0) { 
    continue; 
} 
System.out.println(i+ "**" + count); 

或者,你可以在if (count != 0)封閉System.out.println調用,因爲它是繼continue唯一的語句:link:上ideone

while (number % i == 0) { 
    number /= i; 
    count++; 
} 
if (count != 0) { 
    System.out.println(i+ "**" + count); 
} 

程序。

0

從for循環中刪除if(count == 0){continue;}語句並將其放在for循環中。 :)

for (int i = 2; i<=(number); i++) { 
     count = 0; 
     while (number % i == 0) { 
      number /= i; 
      count++; 
     } 
     if(count==0) continue; 
     System.out.println(i+ "**" + count); 
    } 
0

不確定爲什麼要打印乘法兩次!這裏是清理後的代碼:

public static void printPrimeNumbers(int prime) { 

    int n; 

    for (int i = 2; i <= prime; i++) { 
     n = 0; 
     while (prime % i == 0) { 
      prime /= i; 
      n++; 

     } 

     if (n != 0) { 
      for (int j = n; j > 0; j--) { 
       System.out.print(i); 

       if (prime != 1) { 
        System.out.print("*"); 
       } 
      } 
     } 
    } 
} 
0

你也可以從下面的函數中獲得一些幫助。

public int getPrimeNumber(double number) { 
    int j = 0; 
    while (number % 2 == 0) { 
     number = number/2; 
     j = 2; 
    } 

    for (int i = 3; i <= number; i = i + 2) { 
     while (number % i == 0) { 
      number = number/i; 
      j = i; 
     } 
    } 

    return j == 0 ? 1 : j; 
} 

該函數將返回給定數字的最大素數因子。

0

一方面,您的continue位於while循環內部,對此沒有任何影響。最小的解決將是

public class PrimeFactor { 
    public static void main(String[] args) { 
     System.out.print("Enter a positive number: "); 
     Scanner scanner = new Scanner (System.in); 
     int number = scanner.nextInt(); 
     int count; 
     for (int i = 2; i<=(number); i++) { 
      count = 0; 
      while (number % i == 0) { 
       number /= i; 
       count++; 
      } 
      if (count == 0) { 
       continue; 
      } 
      System.out.println(i+ "**" + count); 
     } 
    } 
} 

但你有一些其他問題:

  • 你的代碼是不正確的「因素」(諷刺的是,「因素」在這方面是指不分成
  • 變量名都很差選擇
  • 功能您(在這種情況下continue)使用goto語句時if就夠

更好的代碼將

public class PrimeFactor { 
    public static void main(String[] args) { 
     System.out.print("Enter a positive number: "); 
     Scanner scanner = new Scanner (System.in); 
     printFactors(scanner.nextInt()); 
    } 
    public static void printFactors(int product) { 
     for (int factor = 2; factor <= product; factor++) { 
      int exponent = 0; 
      while (product % factor == 0) { 
       product /= factor; 
       exponent++; 
      } 
      if (exponent > 0) { 
       System.out.println(factor+ "**" + exponent); 
      } 
     } 
    } 
} 
1
public class _03_LargestPrimeFactor { 

public static void main(String[] args) { 

    long a = 600851475143L; 

    for(int i=2; i<(a/i); i++){       // no factors would exist beyond a/i for a particular i 

     while(a%i == 0){        // if i is a factor 
      a = a/i;         // divide a by i else we wont get a prime number 
      System.out.print(a + " x " + i + "\n"); 
     } 
    } 

    if(a > 1) 
    System.out.println("largest prime factor: " + a); 
} 

} 

控制檯:

8462696833 X 71

10086647 X 839

6857 X 1471

大素因子:6857

+0

詳細解釋它 –