如何在Java中找到带数组的最大数字?

时间:2015-10-13 22:36:43

标签: java arrays integer public

对于这项大学作业,我需要使用public static int indexOfLargestElement(double[] array)

public class FindLargest {

public static void main(String[] args) {

public static int indexOfLargestElement(double[] array) {
    return 0;
}
{
int numbers[] = new int[]{(int) 1.9,(int) 2.5,(int) 3.7,2,(int) 1.5, 6, 3, 4, 5, 2};

    int largest1 = numbers[0];

    for(int i =1;i<numbers.length;i++) {
        int indexOfLargestElement;
            if(numbers[i] > largest1)
            largest1 = numbers[1];
        }
    }


    System.out.println("Largest Element Number is: " + largest1); 

    return.largest; 
}

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

public static void main(String[] args) {
    double numbers []= {1.9,2.5,3.7,2,1.5,6,3,4,5,2};
    int index= indexOfLargestElement(numbers);
    System.out.println("The index of the largest number is: "+index);

}
public static int indexOfLargestElement(double[] array){
    int index=0;
    double largest=0;
    for (int i = 0; i < array.length; i++) {
        if (array[i]>largest){
            largest= array[i];
            index=i;
        }
    }
    return index;
}