To find the

  • Introduction: In Java, we commonly use two types of lookups:
  1. Find seqsearch.java sequentially
  2. 2. Binary search
  • Case demonstration:
  1. There is a number: white eyebrow eagle king, golden lion king, purple shirt dragon king, green wing bat king guess number game: from the keyboard any input a name, determine whether the number contains this name [order search] requirements: if found, prompt to find, and give the lower value.
	// Define an array of strings
	String[] names = {The White-browed Eagle King..The Golden Lion.The King of purple.."The Green-winged Bat King."};
	Scanner myScanner = new Scanner(System.in);
	
	System.out.println("Please enter your name");
	String findName = myScanner.next();
	
	// Go through the number groups, compare one by one, if there is a message, and exit
	// Here is a programming idea/technique, a classic method
	int index = -1;
	for(int i = 0; i < names.length; i++) {
	    // Compare strings to equals if the name is the current element
	    if(findName.equals(names[i])) {
	        System.out.println("Congratulations on finding it." + findName);
	        System.out.println("Subscript =" + i);
	        // Save I to index
	        index = i;
	        break;/ / exit}}if(index == -1) { // Not found
	    System.out.println("Sorry, not found." + findName);
	}
Copy the code



  • My blog is synchronized to tencent cloud + community, invite everyone to come together: cloud.tencent.com/developer/s…