forked from coding-technology/JavaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodTest2.java
More file actions
27 lines (22 loc) · 840 Bytes
/
MethodTest2.java
File metadata and controls
27 lines (22 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class MethodTest2 {
//学生姓名组成的数组(5个) : 指定查找区间 和 查找的名字,返回此人是否存在(如果存在返回位置,否则-1)
//方法参数如何编写? 按需
public static int findNameInStudents(String[] names, int start, int end, String name) {
//{"zs","ls","ww","zl","sq"}
int position = -1;
for (int i = start; i <= end; i++) {
if (names[i].equals(name)) {
position = i;
}
}
return position;
}
public static void main(String[] args) {
String[] names = {"zs", "ls", "ww", "zl", "sq"};
int start = 2;
int end = 3;
String name = "sq";
int result = findNameInStudents(names, start, end, name);
System.out.println(result);
}
}