我寫了一個方法,它將一個int
數組作爲參數,然後返回數組中最大的條目!如何返回Java中最大的數組條目
這就是我所做的,但它沒有奏效!
的唯一錯誤是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;
}
}
你沒有將'array1'作爲參數傳遞給你的'最大'方法。技術上編譯錯誤足以解決這類問題。 –
答案說,你應該'返回array1 [maxValue];'因爲你保存索引在maxValue。 – jlordo