这个用java写的判断素数的程序为什么输不出结果呢?

[复制链接]
查看11 | 回复3 | 2010-5-14 21:31:19 | 显示全部楼层 |阅读模式
public class test {
public static void main(String[] args) {

for(int i=101;i<2000;i++){
boolean isPrimes=true;
for(int j=2;j<=i;j++){

if(i%j==0){

isPrimes=false;

continue;

}

}

if(isPrimes){

System.out.println(""+i);
}
}
}
麻烦写出修改后的代码

回复

使用道具 举报

千问 | 2010-5-14 21:31:19 | 显示全部楼层
if(i%j==0){
isPrimes=false;i%j==0的时候isPrimes=false;而i%j!=0的时候你却没写,就是说isPrimes只有一个植false。而 if(isPrimes){
System.out.println(""+i);是只有当isPrimes为真的时候才被执行。因此输不出结果
回复

使用道具 举报

千问 | 2010-5-14 21:31:19 | 显示全部楼层
一楼把你的错误都说出来了,我就不重复了我给你一个吧public class test {public static void main(String[] args) { int i,j; for(i=101;i<2000;i++) {
for( j=2;j<=i/2;j++)

回复

使用道具 举报

千问 | 2010-5-14 21:31:19 | 显示全部楼层
public class Test { public static void main(String[] args) {for(int i=101;i<2000;i++){ boolean isPrimes=true; for(int j = 2; j <= i/2; j++){
if(i%j == 0){
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行