Java入门级循环与选择问题3 要求有分析...

来源:百度知道 编辑:UC知道 时间:2024/06/27 07:03:40
编写一个程序输出最小的50个素数,输出时每行输出10个数。(类名为PrimeNumber)
素数就是只能被1和本身整除的数
例如 2 3 5 7 ....

谢谢大家了啊..

初学,很不容易,我帮你写一个吧,但是个人建议你要自己锻炼,我也是初学而已!

public class Test{

public static void main(String[] args){
int count = 0;
for(int x=2;x<=50; ++x){
int k=x/2; // 该数的一半
int i = 2; //从2开始
boolean isPrime = true;//素数标记
for(;i<=k;i++){//判断是否素数
if(x%i==0)
isPrime = false; //不是素数
}
if(isPrime) {//输出
System.out.print(x + " ");
count ++;
if(count == 10){
System.out.println();
count = 0;
}
}
}
}
}

才5分,懒得写,我不是助人为乐的人,拿两分走人

楼上的精神很感人··所以我也来赚2分··别恨我··555555555太感人了·