程序分析:判斷素數的方法:用一個數分別去除2到sqrt(這個數),如果能被整除,則表明此數不是素數,反之是素數
public class SingleNum{
public static void main(String[] args) {
int count = 0;
for(int i=101;i<200;i++) {
//默認是素數
boolean flag = true;
for(int j=2;j<=Math.sqrt(i);j++) {
if(i%j == 0) {
//能整除
flag = false;
}
}
if(flag) {
count +=1;
System.out.print(i+",");
}
}
System.out.println("\n有"+count+"個素數");
}
}
輸出結果:

