輸出200以內的所有素數以及素數的總個數?
public class PrimeNumber {
private int count = 0;
private int n;
//用于判斷一個數是不是素數
public boolean judge(int number){
for(int i = 2; i <= Math.sqrt(number); i++){
if(number%i == 0){
return false;
}
}
return true;
}
//用于處理某一范圍的數據
public void handle(int startNumber , int endNumber){
for(int i = startNumber; i <= endNumber; i++) {
if(this.judge(i) == true){
this.count++;
}
}
}
public void show(){
System.out.println("count = " + count);
}
}
=====================================================================
package bbb;
public class PrimeNumberTest {
public static void main(String[] args) {
PrimeNumber pn = new PrimeNumber();
pn.handle(101, 200);
pn.show();
}
}