Write the following methods:
13. A method named loadScores that will be passed the size of an array, prompt the user to enter the appropriate number of floating point grades, then return the loaded array.帶參數的調用方法
我寫了這個方法,但問題出現在我嘗試調用它的時候。下面是我有:
public class ArrayHandout {
public static void main(String[] args) {
int size = loadScores(size);
double[] scoresArray = loadScores(size);
} // end main
public static double[] loadScores(int size) {
Scanner input= new Scanner(System.in);
System.out.println("How many scores would you like to enter?");
size = input.nextInt();
double[] scoresArray = new double[size];
int i;
for(i = 0; i < scoresArray.length; i++) {
System.out.println("Please enter a score:");
scoresArray[i] = input.nextDouble();
}// end for
System.out.println(scoresArray[i]);
return scoresArray;
} // end loadScores
} // end class
我在試圖糾正時,我寫的原代碼,我有一些錯誤,改變了一些事情,不幸的是甚至不記得是什麼這些變化是,或者如果他們固定的因爲我現在無法編譯它的問題。我知道當我能夠編譯原始代碼時遇到的問題是,它只是在數組中打印第一個數字,而不是打印整個數組。就像我說的,我不知道這個問題已經因爲現予以更正,當我嘗試編譯我收到以下錯誤信息:
1 error found: File: C:\Users\HiTechRedneck\Desktop\Fall 2013\PlayingwithJava\ArrayHandout.java [line: 6] Error: incompatible types required: int found: double[]
我知道size
需要在main
聲明,因爲以前我是得到「無法找到變量大小」的錯誤,但我認爲我試圖聲明int size
爲loadScores(size)
這一事實正在拋出它,因爲我聲明的變量也是該方法中的一個參數。
任何幫助將不勝感激。
'int size = loadScores(size);' 這不是你想要的。 「尺寸」不應該只是一個數字,比如5? – kevinsa5
但尺寸取決於用戶在該方法中輸入的內容,這就是爲什麼我卡住了。 –
如果您正在讀取函數內的'size',則不需要將其作爲函數的參數 - 這意味着您在讀取之前知道'size'的值! – kevinsa5