kwinwei
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 + "不是质数");
            //}                
        }
    }

}

分类:

技术点:

相关文章:

  • 2021-11-19
  • 2020-07-08
  • 2022-12-23
  • 2022-01-10
  • 2022-03-03
  • 2022-01-08
  • 2018-08-27
  • 2021-08-31
猜你喜欢
  • 2022-03-07
  • 2021-12-04
  • 2021-05-15
  • 2022-12-23
  • 2021-05-23
  • 2021-04-20
相关资源
相似解决方案