第1个回答 2010-05-06
改成这样。。
public static void main(String[] args)
{
int i=0;
for (int n = 1;n <=9999; n++) {
for(int m = 2;m <= n; m++){
if((n % m==0)&&(n != m)){
i++;
break;
}
}
}
i=10000-i;
System.out.println ("10000以内的质数的个数:"+i);
}
第2个回答 2010-05-06
public class Main {
public static void main(String[] args)
{
int i=0;
int result=0;
int m=0;
for (int n = 1;n <10001; n++) {
for( m = 2;m <10001; m++){
if(n % m ==0){
break;
}
}
if(m==n){
result++;
}
}
System.out.println (result+"<<<<<<");
}
}
这样写应该没有问题了,你试试看
第3个回答 2010-05-06
第二个for循环中把m<9999改成m<n;
第4个回答 推荐于2016-12-01
最简便算法:
public class Main
{
public static void main(String[] args)
{
int t = 0;
for (int i = 2; i <= 10000; i++)
{
int a = 1;
for (int n = 2; n <= Math.sqrt(i); n++)
{
a = a * ( i % n);
if ( i % n >= 2)
{
a = a / 2;
}
}
if (a != 0)
{
t++;
}
}
System.out.println(t);
}
}
你的方法修改为:
public class Main
{
public static void main(String[] args)
{
int i=0;
for (int n = 2;n <=9999; n++)
{
for(int m = 2;m <= n; m++)
{
if(n % m==0&&n!=2&&m!=n)
{
break;
}
else if(m==n-1||n==2)
{
i++;
}
}
}
System.out.println ("10000以内的质数的个数:"+i);
}
}本回答被提问者采纳