寫一個程序,使用兩個嵌套的for循環和取餘操做符(%)來探測和打印素數(只能被其自身和1整除,而不能被其餘數字整除的整數)java
package com.jerry.ch04; public class PrintPrimeNumber { public static void main(String[] args) { for (int i=2; i<100; i++) { boolean flag = true; for (int j=2; j<i; j++) { if (i % j == 0) { flag = false; break; } } if (flag) { System.out.println("prime: " + i); } } // judgmentPrime(2); } public static boolean judgmentPrime(int a) { boolean f = true; if (a == 1) { f = false; } for (int i = 2; i < a; i++) { if (a % i == 0) { f = false; break; } } if (f) { System.out.println(a); } return f; } }