-2

我写了一个将int数组作为参数的方法,然后返回数组的最大条目!

这就是我所做的,但它没有工作!

唯一的错误是largest();

The method largest(int[]) in the type Moon is not applicable for the arguments ()

问题是什么?

public class Moon {
    public static void main(String[] args {     
        int array1[] = {5,10,15,20,25,30};

        int max = largest();
        System.out.println("the largest number is : " + max);

    }


    static int largest( int array1[] ){

        int maxValue = 0;

        for (int i = 0; i < array1.length; i++){
            if (array1[i] > array1[maxValue]) maxValue = i;       
        }
        return maxValue;
    } 
}
4

4 回答 4

3

您实际上并没有将数组作为参数传递。应该:

int max = largest(array1);
于 2012-12-04T21:10:57.897 回答
1
  1. 通过将数组传递为调用最大:

    int max = largest(array1);
    
  2. 将您初始化maxInteger.MIN_VALUE

    int maxValue = Integer.MIN_VALUE;
    
  3. 将您的比较更改为:

    if (array1[i] > maxValue )         
       maxValue = array1[i];         
    }
    
于 2012-12-04T21:10:32.013 回答
1

我看到两个问题:

  1. 您忘记在largest()调用中传递数组。
  2. largest()返回最大值的索引,但调用者将其视为值本身。
于 2012-12-04T21:11:15.720 回答
0

int max = largest(array1);

你的功能:

static int largest(int array1[]){

    int maxValue = Integer.MIN_VALUE;

    for (int el : array1)
        if (el > maxValue)
             maxValue = el;

    return maxValue;
} 
于 2012-12-04T21:09:15.113 回答