写一个程序,使用两个嵌套的for循环和取余操作符(%)来探测和打印素数(只能被其自身和1整除,而不能被其他数字整除的整数)
/** * ****************** Exercise 4 ****************** * Write a program to detect and print prime numbers * (integers evenly divisible only by themselves * and 1), using two nested for loops and the * modulus operator (%). ************************************************ * Created by yule on 2017/10/30 22:16. */
private static void findPrimes(int max) { boolean primes; for(int i = 1; i <= max; i++){ primes = true; //排除自身和1 for(int j = 2; j < i; j++){ if(i % j == 0){ primes = false; } } if(primes){ System.out.print(i + " "); } } }