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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
public class zhachao { public static void main(String[] args) { int[] arr = {1, 8, 6, 9, 51, 24, 36, 95, 78};
Scanner sc = new Scanner(System.in);
System.out.println("输入要查找的数据"); int number = sc.nextInt(); int index = getIndex(arr,number); System.out.println("index:" + index); }
public static int getIndex(int[] arr, int number) { int index = -1;
for (int x = 0; x < arr.length; x++) { if (arr[x] == number) { index = x; break; } } return index; } }
|