习题2(在字符串数组中查找特定字符串的序号)
import java.util.Scanner;
public class Homework {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
A02 a2 = new A02();
String arr[] = {"China","guo","shou","jun"};
System.out.println("请输入要查找的字符串");
String x = myScanner.nextLine();
int res = a2.find(arr,x);
System.out.println(x+"和字符串数组中的第"+(res+1)+"个元素相同");
}
}
class A02 {
public int find(String strs[],String x){
for ( int i = 0; i<strs.length ; i++ ) {
if ( x.equals(strs[i])) {
return i;
}
}
return -1;
}
}