韩顺平JAVA基础学习:简单的数组查找
支持韩老师–学习地址:https://www.bilibili.com/video/BV1fh411y7R8?p=170
数组学习之数组查找:SeqSearch.java
import java.util.Scanner;
public class SeqSearch {
public static void main(String[] args){
String[] arr = {"蜡笔小新","路飞","旋涡名人","李逍遥","王冠越","王霞"};
Scanner myScanner =new Scanner(System.in);
System.out.println("请您输入您要查找的名字:");
String findName = myScanner.next();
int index = -1;
for(int i = 0;i < arr.length; i++){
if(findName.equals(arr[i])){
System.out.println("匹配到您输入的名字:"+arr[i]+"\n"+"数组中的第"+(i+1)+"个元素");
index = i;
break;
}
}
if(index == -1){
System.out.println("未匹配到您输入的名字!");
}
}
}
运行结果!
D:\Java\jdk-9.0.4\bin\java.exe "-javaagent:D:\JetBrains\IntelliJ IDEA Educational Edition 2021.1.1\lib\idea_rt.jar=49681:D:\JetBrains\IntelliJ IDEA Educational Edition 2021.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\basic-java\out\production\day01 lbxx.day05.demo01.SeqSearch
请您输入您要查找的名字:
王霞
匹配到您输入的名字:王霞
数组中的第6个元素