您的位置:首页 > 编程语言 > Java开发

java + 100以内的素数(质数)

2017-02-09 21:34 507 查看
/*
判断100以内的质数
质数:只能被1和它本身除尽(1既不是质数也不是素数)
*/
public class lesson_0_10 {
public static void main(String[] args) {
for (int i = 2; i<=100;i++){
boolean flag = true;
for(int j = 2;j < i ;j++){
if (i % j  == 0){
flag =false;
break;
}
}
if (flag){
System.out.print(i+" ");
}
}
}
}

或者方法2:
public class lesson_0_11 {
public static void main(String[] args) {
for (int x = 2; x<=100;x++){
for (int y=2;y <= x;y++){
if (x%y==0 && x!=y){
break;
}
if(x%y == 0 && x==y){
System.out.print(x+" ");
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: