package HspLearningArray;
import java.util.Scanner;
public class ArrayFind {
public static void main(String[] args) {
//有一个数列︰白眉鹰王、金毛狮王、紫衫龙王、青翼蝠王猜数游戏︰从键盘中任意输入一个名称,判断数列中是否包含此名称【顺序查找】要求:如果找到了,就提示找到,并给出下标
Scanner myscanner = new Scanner(System.in);
String[] arr = {"白眉鹰王","金毛狮王","紫衫龙王","青翼蝠王"};
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("恭喜你找到"+ findName);
System.out.println("下标为"+ i );
Index = i;
break;
}
}
if (Index == -1){//没有找到名称
System.out.println("抱歉,没有找到"+ findName);
}
}
}
请输入名称:
000
抱歉,没有找到000