I didn't mean to see a string of code that I wrote again after I practiced my basic skills at home after I failed an interview.
There are tens of thousands of methods. The following are the most impressive ones for me. Looking back, I still think it's meaningful to learn, so I send them out to share learning.
The original condition of the problem is not remember too Qing, and the general realization is: randomly generate the nine palace pattern (gesture) password
There are several points to read:
1. Password length can be set
2. The path of gesture password must be direct
It can be realized in two ways:
1. Can arr judge whether the value before and after can reach directly
2. getNum gets the next value based on the previous value
package com.maomao; import org.testng.annotations.Test; import java.util.HashMap; import java.util.Map; //Random generated nine grid graphic password, random number range 1~9 public class RandomScret { //9 In the grid, 1~9 Direct numerical value, discovery rule //Except for 5 //Odd number, 1, 3, 7, 9, four corners can't be reached directly //Even numbers, 2, 4, 6, 8, diagonals are not directly accessible, and the difference between diagonals and 5 is the same // int[] num_1={2,4,5,6,8}; // int[] num_2={1,3,4,5,6,7,9}; // int[] num_3={2,4,5,6,8}; // int[] num_4={1,2,3,5,7,8,9}; // int[] num_5={1,2,3,4,6,7,8,9}; // int[] num_6={1,2,3,5,7,8,9}; // int[] num_7={2,4,5,6,8}; // int[] num_8={1,3,4,5,6,7,9}; // int[] num_9={2,4,5,6,8}; //Get 1~9 Random integer of //( n~m Random number formula:(int)(Math.random()*(m-n+1) +n))) public static int getRandomNum(){ int num=(int)(Math.random()*9+1); return num; } //Judge whether it can arrive -----It's a way of trial and error,Takes a long time public Boolean canArr(int preNum,int randomNum){ //Mark, not reachable by default boolean flag = false; //Before and after values cannot be equal if (preNum!=randomNum){ if (preNum==5 ) { flag = true; }else if (randomNum==5){ flag = true; }else if (preNum%2==0){ //It's even int n=Math.abs(preNum-5); int m=Math.abs(randomNum-5); if (n!=m){ flag=true; } }else if (preNum%2==1){ //preNum It's an odd number except five if (randomNum%2==0){ flag=true; } } } return flag; } //Construct a direct path ------This method takes up memory and takes a short time public Map<Integer,int[]> All() { Map<Integer, int[]> map = new HashMap<>(); map.put(1,new int[]{2,4,5,6,8}); map.put(2,new int[]{1,3,4,5,6,7,9}); map.put(3,new int[]{2,4,5,6,8}); map.put(4,new int[]{1,2,3,5,7,8,9}); map.put(5,new int[]{1,2,3,4,6,7,8,9}); map.put(6,new int[]{1,2,3,5,7,8,9}); map.put(7,new int[]{2,4,5,6,8}); map.put(8,new int[]{1,3,4,5,6,7,9}); map.put(9,new int[]{2,4,5,6,8}); return map; } public int getNum(int preNum){ // int tag=(int)(Math.random()*10); int[] nextArr=All().get(preNum); int tag=(int)(Math.random()*10+1);//1~10 int next=nextArr[tag%nextArr.length]; return next; } @Test public void test1(){ RandomScret rs=new RandomScret(); //Set password length length int length=6; int[] scret=new int[length]; //Specify build int preNum=getRandomNum(); for (int i=0;i<length;i++){ scret[i]=rs.getNum(preNum); preNum=scret[i]; } for (int num:scret){ System.out.println(num); } } @Test public void test2(){ RandomScret rs=new RandomScret(); //Set password length length int length=6; int[] scret=new int[length]; //Random generation int preNum=getRandomNum(); for (int i=0;i<length;){ int randomNum=getRandomNum(); if (rs.canArr(preNum,randomNum)){ scret[i]=randomNum; preNum=scret[i]; i++; }else { continue; } } for (int num:scret){ System.out.println(num); } } }