Java中的数组很新,但我给出的问题是:
“在您的main方法中,提示用户输入十个数字并将它们存储在一个数组中 . 编写一个名为find的方法,返回数组中特定数字第一次出现的位置 . 如果该数字不在列表中,你的方法应该返回-1 . 从main,提示用户输入一个数字,调用find方法,并从main显示结果给用户 . 如果方法返回-1,告诉用户该数字不在不要在find方法中与用户交互 . 方法的签名必须是:public static int find(int [] arr,int thingToFind)“
输出示例:
Enter ten numbers: 18 14 82 17 2 14 6 2 18 4
What number would you like me to find? 2
2 first occurs in the 5th place in the list.
Enter ten numbers: 4 19 0 41 2 7 7 14 41 100
What number would you like me to find? 99
99 is not in the list.
我知道我需要一个循环来进入find方法,但不确定哪一个,我正在考虑for循环但不知道如何实现它 . 这是我现在的代码,是的,我知道我错过了很多,而这只是它的开始 . 不知道从哪里开始 .
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 10 numbers ");
int n = in.nextInt();
int arr[] = new int[n];
}
public static int find(int[] arr, int thingToFind){
}
}