求打印101到200的所有质数。
以下是本人写的,问什么打印不出来?
public class test {
public static void main(String []args){
int m;
int j=0;
int low=101,high=200;
for(int i=low;i<=high;i+=2){
for(m=2;m<i;m++){
if(i%m==0)
break;
if(i==m)
System.out.print(i+" ");
}
j++;
if(j%5==0)
System.out.println();
}
}
}
------解决方案--------------------
- Java code
public static void main(String[] args) throws Exception { int m; int j = 0; int low = 101, high = 200; for (int i = low; i <= high; i += 2) { boolean bool = false; for (m = 2; m < i; m++) { if (i % m == 0) { bool = true; break; } // if (i == m) // System.out.print(i + " "); } if (!bool) { System.out.print(i + " "); j++; if (j % 5 == 0) System.out.println(); } } }
------解决方案--------------------
- Java code
public static void main(String[] args) throws Exception { int count = 0; for (int i = 101; i < 200; i += 2) { if (prime(i)) { count++; System.out.print((count % 5 == 0) ? i + "\n" : i + " "); } } } public static boolean prime(int num) { if (num <= 1) { return false; } int sqrt = (int) Math.sqrt(num); for (int i = 2; i <= sqrt; i++) { if (num % i == 0) { return false; } } return true; }
------解决方案--------------------
public class Prime()
{
public static void main(String args[])
{
int i,j,k;
System.out.println("100到200的质数有:");
for(int m=101;m<=200;m+=2)
{
k=(int)(Math.lanage.aqrt(m));
for(int j=2;j<k;j++)
if(m%j==0)
System.out.print(m+" ");
else if(j>k)
break;
}
}
}