Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java数组:查找数组中存储值的索引。是_Java_Arrays - Fatal编程技术网

Java数组:查找数组中存储值的索引。是

Java数组:查找数组中存储值的索引。是,java,arrays,Java,Arrays,我需要创建一个程序,让用户输入存储在数组中的10个数字。我已经有了确定用户输入的最小值和最大值的代码,但我遇到的问题是如何显示最大值和最小值所在的索引 这是我的密码: import java.util.Scanner; public class Array { static Scanner in = new Scanner(System.in); public static void main(String[] args) { int numbers[]

我需要创建一个程序,让用户输入存储在数组中的10个数字。我已经有了确定用户输入的最小值和最大值的代码,但我遇到的问题是如何显示最大值和最小值所在的索引

这是我的密码:

import java.util.Scanner;

public class Array {

    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {

        int numbers[] = new int[10];
        int smallest = Integer.MAX_VALUE, largest = numbers[0];

        for(int i = 0; i < 10; i++){ 
//I get the "Can't find symbol error" on this part: index = i;

            index = i;
            System.out.print("Array Number " + i + ": ");
            numbers[i] = in.nextInt();
        }
        for (int n = 0 ; n < numbers.length; n++) {
            if (numbers[n] < smallest) {
                smallest = numbers[n];
            } 
            if (numbers[n] > largest) {
                largest = numbers[n];
            }      

        }

//And this part, which it the: index

        System.out.println("Maximum number is " + largest + " located in index " + index);
        System.out.println("Minimum number is " + smallest + " located in index " + index);

    }   

}
我想知道我弄错了哪个部分。

索引没有定义,因此您在那里得到了一个错误

int index = i;
为了找到最小和最大数字的索引,您需要两个索引,每当您找到较小或较大的值时,都会更新这些索引。您甚至不需要保存最小值和最大值,因为它们存储在数组中,并且可以使用两个索引进行检索,这要感谢Andrew S.提到:

int maxIndex = -1, minIndex = -1;

for (int n = 0 ; n < numbers.length; n++) {
    if (numbers[n] < smallest) {
        smallest = numbers[n];
        minIndex = n;
    } 
    if (numbers[n] > largest) {
        largest = numbers[n];
        maxIndex = n;
    }      
}

System.out.println("Maximum number is " + numbers[maxIndex]+ " located in index " + maxIndex);
System.out.println("Minimum number is " + numbers[minIndex] + " located in index " + minIndex);

未定义索引。删除索引=i;注意在这里我基本上被用作你的索引你使用的是相同的变量索引还是你希望用它来告诉你最高和最低的索引?您需要两个不同的变量。请注意,将索引跟踪为minIndex和maxIndex足以确定实际值。最小值是数字[minIndex],最大值是数字[maxIndex],因此不需要最小值和最大值。