[20-04-26][Self-test 2]Java Prime Number

Posted mirai3usi9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[20-04-26][Self-test 2]Java Prime Number相关的知识,希望对你有一定的参考价值。

 1 package test_1_2;
 2 
 3 public class PrimeNum {
 4 
 5     public static void main(String[] args) {
 6 
 7         /** 判断101-200之间有多少个质数,并输出所有质数。 */
 8         
 9         int count = 0;
10         
11         for (int i = 100; i < 201; i++) {
12             if (isPriNum(i) == true) {
13                 System.out.print(i + ",");
14                 count++;
15             }
16         }
17         
18         System.out.println("共计" + count + "个");
19         
20     }
21 
22     /** 判断是否为质数 */
23     private static boolean isPriNum(int num) {
24         
25         for (int i = 2; i <= num / 2; i++) {
26             if (num % i == 0) {
27                 return false;
28             }
29         }
30         
31         return true;
32     }
33     
34 }

 

结果如下:

101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,共计21个

以上是关于[20-04-26][Self-test 2]Java Prime Number的主要内容,如果未能解决你的问题,请参考以下文章

[20-04-27][Self-test 11]Java CountBonus

[20-05-02][Self-test 32]Java GuessNum

[20-05-09][Self-test 40]Java BankSystem 1

[20-05-09][Self-test 40]Java BankSystem 1

[20-04-30][Self-test 25]Java MaxAndMin

[20-04-30][Self-test 24]Java SecondMax