判断质数(Java)
package day01; //输出1-100中质数,并且每十个换行 public class PrimeNum { public static void main(String[] args) { //并非一次判断用开关 int count = 0; for(int i = 2 ; i <= 100;i++) { boolean flag = true; //1、假设是质数 for(int j = 2; j <= (int)Math.sqrt(i); j++) { //Math.squart()取平方根 if(i % j == 0) { flag = false; //2、改为不是质数 break; } } if(flag) { //3、得结论 count++; System.out.print(i + ","); if(count%10 == 0) System.out.println(); } //else { // System.out.println(i + "不是质数"); //} } } }