我正在尝试在数组中查找一个值,并且我确定该值将只存在一次,因此我正在尝试查找该值并返回存储它的数组的索引,如果未找到则返回 -1 这个是我想要做的:
static Integer[] accs = new Integer[20];
public static int search()
{
Integer[] numbers;
numbers = accs;
Integer key;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Account Number:");
key = sc.nextInt();
for (Integer index = 0; index < numbers.length; index++)
{
if ( numbers[index] == key )
return index; //We found it!!!
}
// If we get to the end of the loop, a value has not yet
// been returned. We did not find the key in this array.
return -1;
}
即使我知道该值存在于数组中,当我运行它时,也没有显示任何内容。我进行了调试,然后我发现 Key 变量没有我输入的值。有什么问题吗?