把isPrime改成:
boolean isPrime(long checkNumber)
{
double root=Math.sqrt(checkNumber);
for(int i=2;i<=root;i++)
{
if(checkNumber%i==0)
{
return false;
}
}
return true;
}
注意看return true的位置
boolean isPrime(long checkNumber)
{
double root=Math.sqrt(checkNumber);
for(int i=2;i<=root;i++)
{
if(checkNumber%i==0)
{
return false;
}
}
return true;
}
注意看return true的位置
{
public static void main(String[] args)
{
long x=2;
while(x<=100)
{
if(isPrime(x))
{
System.out.print(x++ );
}
}
}
static boolean isPrime(long checkNumber)
{
double root=Math.sqrt(checkNumber);
for(int i=2;i<=root;i++)
{
if(checkNumber%i==0)
{
return false;
}
}
return true;
}
}打印结果:23
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/class PrimeFinder
{
public static void main(String[] args)
{
long x=2;
PrimeFinder aa=new PrimeFinder();
while(x<=100)
{
if(aa.isPrime(x))
{
System.out.print(x++ ); }
} }
boolean isPrime(long checkNumber)
{
double root=Math.sqrt(checkNumber);
for(int i=2;i<=root;i++)
{
if(checkNumber%i==0)
{
return false;
} }
return true;
}}
class PrimeFinder
{
public static void main(String[] args)
{
int x=2;
while(x<=100)
{
if(!isPrime(x))
{
System.out.println(x++ );
}
else
x++;
}
}
static boolean isPrime(int checkNumber)
{
double root=Math.sqrt(checkNumber);
for(int i=2;i<=root;i++)
{
if(checkNumber%i==0)
{
return true;
}
}
return false;
}
}