## Methods of finding prime numbers

- First of all, understand what prime numbers are: numbers that cannot be divided by whole except 1 and itself!

After understanding this point, we have ideas. FOR example, FOR a number 101, do I want to compare it with 2 until 100 to see whether the result of its division is 0, IF(101%i == 0), then we will directly break, indicating that this number is not a prime number, and we continue to the next 102... And so on. Therefore, we only use the two methods learned, one is FOR and the other is IF.

public class DAY3_AnLi { public static void main(String[] args) { /*Finding prime numbers: the most important thing is to know what prime numbers are. Numbers that can be divided by whole except one and itself are called prime numbers, so we just need to judge whether their division with these numbers is 0, * If it is 0, it directly jumps out of the loop, indicating that this number is not a prime number, * If it is not 0, after the loop is completed, the prime number is output through the conditional size. * */ System.out.println("Prime number has:"); int i,k; for (i = 101; i < 201; i++) { for (k = 2; k < i; k++) { if (i % k == 0) { break; } } if (k == i) { System.out.print(i + "\t"); } } } }

## Application of ASCII code, case, number to character

- The verification code system is an example of using the Title Technology I learned today. Next, I will analyze how to use it and what the idea is.
- For example, I want to generate a 5-digit verification code. For the scalability of the code, I wrote a method to pass in the verification code of several digits you want to generate, and then pass the number.
- How to generate the verification code? In fact, I think there are the following points:

1. First, control the number of characters generated according to the number passed in, and then Return it as a string The For loop should be used here. Each loop produces a character char.

2. Then there is the specific content of the For loop. How can I make this character a number or a case letter? At this time, I think of the function Random. How long should the Random object be passed in? According to the verification code, there are only numbers, uppercase English and lowercase English, we pass in 3, that is, randomly generate a number between 0 and 2, If it is 0, we define it as an uppercase letter, if it is 1, we define it as a lowercase letter, and if it is 2, we define it as a number.

3. Then, when we are sure, for example, this bit will be an uppercase letter. We know that the decimal system of the smallest uppercase letter in ASCII is 65. What we need to do is to generate A 0-26 (there are 26 English letters, 65 is A and 90 is Z), and then add the two and convert them into char type.

3. Then generate a number. How can we convert it into a number, case? The conversion relationship of ASCII code is used here.

Numbers don't need to be converted into characters. Common sense shows that what we enter is still numbers.

The relationship between numbers and capital letters is to add 65 to the original number, and just click char.

The relationship between number to lowercase letter is 97 is A. We only need to generate a number from 0 to 25, and then add the two to force conversion.

- After understanding the above principles, we only need to put the generated characters or numbers together (that is, define an empty string type variable outside the loop, and add the generated string to generate the verification code each time) and Return it.

// Call verification code program System.out.println("Please enter the number of verification codes you want to generate:"); Scanner scan = new Scanner(System.in); int num = scan.nextInt(); System.out.println(NumMa(num)); /*Generate verification code*/ public static String NumMa(int num) { String code = ""; Random s = new Random(); for (int i = 0; i < num; i++) { int type = s.nextInt(3); switch (type) { case 0: //number code += s.nextInt(10); break; case 1: //capital char c = (char) (s.nextInt(26) + 65); code += c; break; case 2: //Lowercase letters char c1 = (char) (s.nextInt(26) + 97); code += c1; break; } } return code; }

Error prone points:

1. When code=code+xx in case, if you want to change it to code+=xx, remember to delete the code after the original equals sign.

2. There is a break behind each case; Form a habit, write case and then knock break first;

## Copy of array

// Call the copy array program int old[] = {20, 30, 40}; int[] newSuzhu=new int[old.length]; copy1(old,newSuzhu); /*Copy of array*/ public static void copy1(int[] old, int[] newSuzhu) { System.out.print("The original array is:"); for (int i = 0; i < old.length; i++) { newSuzhu[i] = old[i]; System.out.print(old[i]+"\t"); } System.out.print("The copied array is:"); for(int i=0;i<newSuzhu.length;i++){ System.out.print(newSuzhu[i]+"\t"); } }

## Review array sorting bubble sorting

for (int i = 0; i < score.length; i++) { for (int k = i + 1; k < score.length; k++) { if (score[i] > score[k]) { int temp = score[i]; score[i] = score[k]; score[k] = temp; } }

## Maximum value of array, average value

int score[] = {20, 30, 40}; // Find out the highest score and lowest score in the array and find the average score Score(score); public static void Score(int[] score) { //10 20 5 40 50 60 6 judges int sum = 0; for (int i = 0; i < score.length; i++) { for (int k = i + 1; k < score.length; k++) { if (score[i] > score[k]) { int temp = score[i]; score[i] = score[k]; score[k] = temp; } } sum += score[i]; } System.out.println("\n"); System.out.println("The lowest score is:" + score[0] + "The highest score is:" + score[score.length - 1] + "The average score is:" + (double)((sum - score[0] - score[score.length - 1]) / (score.length - 2) )); }

- Another way to find the best value

For (int x = 0; x < score. Length; X + +) {/ / loop to find the maximum and minimum if (score [x] > max) {/ / judge whether the subsequent elements are larger than max, max = score[x]; / / if it is larger, modify the max content} if (score [x] < min) {/ / judge whether the subsequent elements are smaller than min, min = score[x]; / / if it is smaller, modify the Min content}

## Exchange the positions in the array, and change the positive order to the reverse order

The specific implementation is: use an empty cup to store the first number or the last number, and then the position of the original number is empty. At this time, put the value of the first number (if the empty cup is placed in the last number) under this position. After placing, put the last number in the position where the first number is empty. Then, when to stop? For example, if there are five numbers, you only need to exchange them several times. Is it enough to exchange them twice, so the condition can also be changed to K < 2. Conclusion: the maximum number of execution times of the loop is the array length / 2.

Q: what does the condition K < I mean?

A: in fact, it's obvious that when the smallest serial number at the beginning is greater than the largest serial number at the beginning, you don't have to exchange, and the exchange has been completed

for(int k=0,i=nums.length-1;k<i;i--,k++){ int temp=nums[i]; nums[i]=nums[k]; nums[k]=temp; }

## Knowledge point memory

- Knowledge point memory: the function of break in which cycle body is to jump out of the current cycle body.
- Knowledge point memory: if you want to return an array, don't forget to add [].
- Memory of knowledge points: if you want to randomly generate numbers from 1 to 20, r.nextint(20)+1.
- Memory of knowledge points: you may not be able to determine the desired value in a cycle, so you can define a flag, which can realize that after the cycle is completed, you only need if (flag) to determine whether to use the value.

## Two color ball system

- Knowledge point 1: randomly generate a set of data without repetition and skillfully use flag.
- Knowledge point 2: Arrays.equals(lucknums,userinput) is used for comparison between arrays

/*main function content of two-color ball system development*/ int[] luckNums = crateNum(); int count = 1; for (int luckNum : luckNums) { System.out.print("The first" + count + "The winning number is:" + luckNum + "\n"); count++; } //Get the number purchased by the user int[] userNums = userInputNumbers(); System.out.print("Your final number is:"); for (int userNum : userNums) { System.out.print(userNum + "\t"); } System.out.println("\n"); // Compare the numbers to determine whether you win the prize judge(luckNums,userNums); }

/*Development of two-color ball*/ public static int[] crateNum() { int[] nums1 = new int[7]; Random ran1 = new Random(); for (int i = 0; i < nums1.length; i++) { // Generate random numbers between 1-33 and cannot repeat 10, 20 and 30 while (true) { int nums2 = ran1.nextInt(33) + 1; boolean flag = true; for (int k = 0; k < i; k++) { if (nums2 == nums1[k]) { flag = false;/*The reason why flag is used here is that you need to traverse the entire array to see if it can be added, rather than adding else if it is not found*/ break;/*Here is the function of jumping out of for. After jumping out, while is still executed until non repeating numbers are randomly selected*/ } } if (flag) { nums1[i] = nums2; break;/*Here is the function of jumping out of while*/ } } } return nums1; } //User input 7 numbers system public static int[] userInputNumbers() { int[] InputeNumbers = new int[7]; Scanner s1 = new Scanner(System.in); for (int k = 0; k < InputeNumbers.length; k++) { System.out.println("Please enter a number(1-33,Do not repeat!):"); while (true) { int temp = s1.nextInt(); boolean flag = true; for (int j = 0; j < k; j++) { if (temp == InputeNumbers[j]) { flag = false;/*The reason why flag is used here is that you need to traverse the entire array to see if it can be added, rather than adding else if it is not found*/ System.out.println("Enter duplicate numbers!!!,Please re-enter:"); break;/*Here is the function of jumping out of for*/ } } if (flag) { InputeNumbers[k] = temp; break;/*Here is the function of jumping out of while*/ } } } return InputeNumbers; } // Compare the number entered by the user with the winning number public static void judge(int[] lucknums, int[] userinput) { if(Arrays.equals(lucknums,userinput)){ System.out.println("Congratulations on winning the prize!!!"); } else{ System.out.println("unfortunately!"); } }

I wanted to continue to write: JAVA object-oriented programming. Later, I think object-oriented is more important. I will open a separate article tomorrow to study together.