java输出素数

来源:百度知道 编辑:UC知道 时间:2024/06/15 20:28:47
public class J_inttobinary {
public static void main(String arg[]){
int temp=0;
int NUM=0;
for(int i=50;i<=100;i++)
{
for(int j=2;j<=i/2;j++){
if((i%j)==0)
temp=1;
}
if(temp==0){
NUM++;
System.out.println("从50到100的第"+NUM+"个素数为"+i);
}
}
}

}

// 你忘了把temp = 0
public class J_inttobinary {
public static void main(String arg[]) {
int temp = 0;
int NUM = 0;
for (int i = 50; i <= 100; i++) {
for (int j = 2; j <= i / 2; j++) {
if ((i % j) == 0)
temp = 1;
}
if (temp == 0) {
NUM++;
System.out.println("从50到100的第" + NUM + "个素数为" + i);
}
temp = 0;
}
}

}

public class J_inttobinary
{
public static void main(String arg[])
{
int temp=0;
int NUM=0;
for(int i=50;i<=100;i++)
{
for(int j=2;j<=i/2;j++)
{

if((i%j)==0)
{
temp=1;
}

}
if(temp==0)
{
NUM++;
System.out.println("从50到100的第"+NUM+"个素数为"+i);
}
temp = 0;//这里必须对temp重新初始化
}
}
}

我已经给你改好了,是你的temp变量的问题
public