Java :计算 100以内的素数并保存在数组中

素数会求,关键是如何保存在数组中~~新手求教!

其他的不写了

保存到数组里
定义数组
int buf[100];
int count = 0;
if(....) //a是素数 {
buf[count] = a;

count ++;

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-12
public class ss{
public static void main(String[] args) {
int flag,i,j;
for(i=1;i<=100;i++)
{
flag=1;
for(j=2;j<i;j++)
{
if(i%j==0){flag=0;break;}
}
if(flag==1)System.out.println(i);
}
}
}
第2个回答  2014-06-12
class MyMath{
public static void main(String[]args){
for(int i=0;i<=100;i++){
if(SuShu(i)){
System.out.println(i);
}
}

}

public static boolean SuShu(int num){
for(int n=2;n<=Math.sqrt(num);n++){
if(num%n==0){
return false;
}
}
return true;
}
}
第3个回答  2014-06-12
package airthmatic;
public class demo8 {
/**
* 素数是指因数只有1和本身的数字
* @param arg
*/
public static void main(String arg[])
{
for(int i=1;i<=100;i++)
{
if(find(i))
System.out.print(i+" ");
}
}
/**
* 1-n个自然数中的素数
* @param n
* @return
*/
public static boolean find(int n)
{
for(int i=2;i<=Math.sqrt(n);i++)
{
if(n%i==0)
return false;
}
return true;
}
}

这是打印出来的。你放到数组里面就可以了。
相似回答