求教各位大神,输出素数
java吧
全部回复
仅看楼主
level 4
惜墨无竹 楼主
不知道哪里错了[乖]
2016年04月30日 09点04分 1
level 8
main方法呢?
2016年04月30日 09点04分 2
在上边吗,我没有截到
2016年04月30日 09点04分
level 6
i%j不等于0就输出?
2016年04月30日 09点04分 3
啥?应该怎么改呢?我的问题是重复输出,一个数出来好几遍[泪]
2016年04月30日 09点04分
我现在没开机,一会给你写一下
2016年04月30日 09点04分
@断漄life 好的好的 谢谢!!
2016年04月30日 09点04分
level 6
你要全都不等于0才能输出啊
2016年04月30日 09点04分 4
什么意思[泪]我的意思是余数不为0,就是说不能整除的意思
2016年04月30日 09点04分
怎么办 我觉得好难[泪]
2016年04月30日 09点04分
@惜墨无竹 比如你要判断101是不是素数,你只判断了101%2是不是为0就输出了,还有101%3,101%4等等没判断呢[汗]
2016年04月30日 09点04分
我的第2层for循环里边 是2~这个数的平方根 我想让他从2~sqrt都走一遍 该怎么弄呢?
2016年04月30日 09点04分
level 6
@Test
public void test03() {
for (int i = 101; i < 201; i++) {
for (int j = 2; j < Math.sqrt(i); j++) {
if (i % j != 0) {
if (j == (int) Math.sqrt(i)) {
System.out.println(i);
}
} else {
break;
}
}
}
}
2016年04月30日 10点04分 5
复制过来就这样了,[汗],你可以放到eclipse运行一下,有问题再说
2016年04月30日 10点04分
@断漄life 谢谢谢谢!太感谢了!
2016年04月30日 10点04分
level 5
你可以加一个boolean值判断一下就可以了
2016年04月30日 10点04分 6
level 2
定义一个布尔变量,false就break continue 为真就输出
2016年04月30日 11点04分 8
level 8
我学历低,但我觉得判断是不是素数写的是不是不对啊,if i%j!=0,J目前是2,意思是不是101到200之间的数先除2,余是不是非0,只要是奇数就不是0啊,那就输出了,135就不是素数啊,
2016年04月30日 11点04分 9
level 3
int count =0;
for(int i =101;i<201;i++){
boolean a = true;
for(int j = 2;j<i;j++){
if(i%j==0){
a= false;
break;
}
}
if(a){
System.out.println(i);
count++;
}
}
System.out.println(count);
2016年04月30日 11点04分 10
1