[Photo + Video] Java based on the classical exercises (a) output prime number between 2 to 100, and the number of prime numbers

Code can not solve the problem can be written once and
we need to find the lack of debug mode and then make changes after our idea to write
code that can be obtained only after several tests to solve the problem!
Through the study, practice exercises based on the classic [Java], let us work together to cultivate this thinking to solve the problem.

The first question: How many primes between 2-100 determination, and outputs all primes.

1. Video Tutorial:

Thinking between 2-100 prime requirements
demand a prime number between 2-100 Code Explanation

2. The idea of ​​analysis:

Q1: What is a prime number?
A1: prime (prime number) refers to the natural number greater than 1, in addition to 1 and the number itself outside , can not be divisible by the number of other natural numbers. (That is to say a number 1 and itself only two factors )

Q2: How to tell if a number has in addition to 1 and the number itself a factor?
A2: Let us take an example to analyze 4:

  • Determines whether a number divisible by four, nothing but only the following four cases:
    Case 1: 4/1
    Case 2: 4/2
    Case 3: 4/3
    Case 4: 4/4
  • So we know: Each number 1 and itself has two factors (ie, each number can be divisible by 1 and itself)
    So we have a number divisible by judging whether [4] only need to consider the following In both cases:
    case 2: 4/2
    Scenario 3: 4/3
  • Thus, we can sum up rule:
    determining whether a number of other factors , to make the number of removed [2- (determined required number -1)] The number within this range .
    The determination only needs to look to 4: 4 / 2,4 / 3

  • To see if there is divisible happens both cases:
    if there is a description of this number is not a prime number , the next will be a judge
    if there is no description for this number is a prime number , to be the output of this number, and let save [variable number of primes] plus 1 is determined once again.

    3. Code + + Detailed answer:

package Exercise;

public class SuShu1 {

    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        int count = 0;//储存2-100之间的素数个数
        boolean flag = true;
//用处:在当一个数有【除1和它本身之外的因数】时,使其值变为false,以便进入下一次循环
        for (i = 2; i <= 100; i++) {//从2-100开始进行循环
            flag = true;
//每完成一次for循环需要把flag值重新置为true,否则将会影响下次的循环
            for (j = 2; j < i; j++) {
//用此for循环的数字来判断i是否有【除1和它本身之外的因数】
                if (i % j == 0) {//如果i%j==0,说明i有【除1和它本身之外的因数】
                    flag = false;//这时让flag = false;
                    break;//并退出这个双重for循环
                }
            }
            if (flag == true) {
//从双重for循环退出后,判断flag的值是否是true,若为true则说明此数为素数
                count++;//存储素数的总和加1
                System.out.println("从2-100之间的素数有:" + i);//输出素数结果
            }
        }
        System.out.println("从2-100之间的素数个数有:" +count);//输出素数总个数
    }

}

Code explanation: In Case i = 4

  1. i=4 4<100
  2. flag = true
  3. j=2 ,2<4
  4. Because i% j == 0 (i.e., 4% 2 == 0) so flag = false. And execute the break statement.
  5. Execution break statement after the exit for the second cycle. (After performing break statement, the code behind the break statement will not execute and terminate the present cycle layer)
  6. Because now the flag == false is not satisfied if (flag == true) condition
  7. So i ++ i = 5, the time is determined to enter

result:
Here Insert Picture Description

4. We prepare the eggs:

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/Qpgshare/p/12516390.html