【发布时间】:2016-02-28 01:17:29
【问题描述】:
对 Java 中的数组非常陌生,但我要解决的问题是:
"在你的main方法中,提示用户输入十个数字并存储 他们在一个数组中。编写一个名为 find 的方法,返回位置 数组中特定数字的第一次出现。如果 number 不在列表中,您的方法应该返回 -1。从主要, 提示用户输入数字,调用 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){
}
}
【问题讨论】:
标签: java