-4
這不是一個家庭作業問題。這是我的做法之一。請幫助我瞭解我做錯了什麼。原來是static void changeArray,但是我將它改爲static int changeArray並在最後插入了一個return語句,但它仍然不會更新主代碼。我的退貨聲明和靜態方法有什麼問題
公共類的測試{
/*
* Change the method to also update the key at the main
*/
static int changeArray(int key, int array[]){
key = key + 7;
for (int i = 0; i < array.length; i++){
array[i] = array[i] + key;
}
System.out.println("*At changeArray *");
System.out.println("The key is: "+ key);
return key;
}
static void printArray(int array[]){
System.out.print("[ ");
for (int element:array){
System.out.print(element + " ");
}
System.out.println("]");
}
public static void main(String[] args){
int key = 5;
int[] array = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
System.out.println("*At the main *");
System.out.println("The key is: "+ key);
printArray(array);
changeArray(key, array);
System.out.println("*At the main *");
System.out.println("The key is: "+ key); <--- (this is supposed to be 12 after the method is called, but it keeps printing out 5)
printArray(array);
}
}
您沒有將'changeArrray()'的返回值賦值給任何東西。它應該被分配到你的主要方法中的'key'。 –