Alex-Hsing 發問時間： 電腦與網際網路程式設計 · 1 0 年前

# Java 問題~ 程式需修正 很很很 很急 埃拉托斯特

The main problem is that multiples of prime number higher or equal to 5 are not deleted but printed out as primes

======================================================

import java.util.*;

import java.lang.Math.*;

/**

* A class for the calculation part of the prime number generator.

*

* @author Douglas Cheung

* @version Aug 12, 2008

*/

public class Sieve

{

/**

* The only method in the class doing the calculation for prime numbers.

*

* @return primelist The list of prime numbers generated.

*/

public static ArrayList<Integer> findPrimes(int n)

{

ArrayList<Integer> numberlist;

numberlist = new ArrayList<Integer>();

for(int i = 2; i <= n; i++) {

}

ArrayList<Integer> primelist;

primelist = new ArrayList<Integer>();

for(int j = 0; numberlist.get(j) < Math.sqrt((double)n); j++) {

int currentPrime = numberlist.get(0);

Iterator<Integer> it = numberlist.iterator();

while(it.hasNext()) {

int num = it.next();

if(num % currentPrime == 0) {

it.remove();

}

}

}

Iterator<Integer> it = numberlist.iterator();

int num;

while(it.hasNext()) {

num = it.next();

it.remove();

}

return primelist;

}

}

==================================================

### 2 個解答

• 志明
Lv 5
1 0 年前
最佳解答

compile不過= =

2008-08-13 14:11:14 補充：

回錯題了拍勢…

2008-08-13 14:47:55 補充：

邏輯沒錯吧…你想要的答案是什麼？

2008-08-13 16:38:38 補充：

把Driver.java的25行改成這樣

int percent = (int) Math.round(((double)size / n) * 100);

資料型別的問題

2008-08-13 16:40:37 補充：

我英文很爛…所以不知我講的對不對…因為1~100有34個質數…所以不是25%而是34%

2008-08-13 17:04:26 補充：

答錯了，等我一下

2008-08-13 17:49:21 補充：

把for (int j = 0; numberlist.get(0) < Math.sqrt((double) n); j++) {

改成while(numberlist.get(0) < Math.sqrt((double) n)){

<< >>

2008-08-13 17:52:23 補充：

把for (int j = 0; numberlist.get(j) < Math.sqrt((double) n); j++) {

改成while(numberlist.get(0) < Math.sqrt((double) n)){

在Driver的檔案裡

2008-08-13 18:20:43 補充：

總結：

把for (int j = 0; numberlist.get(j) < Math.sqrt((double) n); j++) {

改成while(numberlist.get(0) < Math.sqrt((double) n)){

因為你程式判斷錯了!!!

PS:你英文好強…教人嗎？

參考資料： myself
• 1 0 年前

here are problem is :

i.e. for n = 100, there are 25 prime numbers

then the % will be 25%

I need to show the percentage before the prime numbers printed out

however, somehow it always comes out to be 0%

hope you can help this , please ~

感謝 智民!!

2008-08-14 12:25:42 補充：

No, This description of the problem is written by my friend. I just copy it and post it on.

If you are interested in English , you could check here ( http://a4esl.org/ ). My Englsih is

not good, either. ^^"