2013-12-11 65 views
0

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 sizeloadScores(size)這一事實正在拋出它,因爲我聲明的變量也是該方法中的一個參數。

任何幫助將不勝感激。

+2

'int size = loadScores(size);' 這不是你想要的。 「尺寸」不應該只是一個數字,比如5? – kevinsa5

+0

但尺寸取決於用戶在該方法中輸入的內容,這就是爲什麼我卡住了。 –

+0

如果您正在讀取函數內的'size',則不需要將其作爲函數的參數 - 這意味着您在讀取之前知道'size'的值! – kevinsa5

回答

0

您的線路int size = loadScores(size)不正確。 loadScores返回類型爲double[],因此您需要爲此分配返回值。

只要刪除那一行,一切都應按預期工作。

0

int size = loadScores(size);拋出一個錯誤,因爲loadScores返回一個double類型的數組。

由於任務只是想要一個代碼片段你很可能給大小任意值,並把它傳遞或者你可以在主擺脫大小的,只是經過了一些:

public static void main(String[] args) { 
    double[] scoresArray = loadScores(5); 
} 

編輯:另外值得注意的是,在for循環之後的print語句會拋出一個ArrayIndexOutOfBoundsException,因爲我現在比數組長度更長。如果要打印scoresArray的內容,則需要另一個循環遍歷每個元素。

第二次編輯:如果您提示用戶輸入一個大小,您應該在main方法中運行提示符,然後將其傳遞給loadScores()。

0

調用函數,你在函數 讀它之前,您不需要在大小你所有的聲明可能是:

public static double[] loadScores() 

,並在主調用

loadScores() 

。例如:

public class ArrayHandout { 
    public static void main(String[] args) { 
    double[] scoresArray = loadScores(); 
    //do whatever you want here or change declaration of 
    //the function to void and do not return anything 
    } 

    public static double[] loadScores() { 
    Scanner input= new Scanner(System.in); 

    System.out.println("How many scores would you like to enter?"); 
    int 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;  
    } 
} 

,如果你不使用返回數組倒不如去做這樣的:

public class ArrayHandout { 
    public static void main(String[] args) { 
    loadScores(); 
    } 

    public static void loadScores() { 
    Scanner input= new Scanner(System.in); 

    System.out.println("How many scores would you like to enter?"); 
    int 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]); 
    } 
} 

(當一個方法不返回任何東西你必須指定它在簽名中使用void關鍵字) - 您還需要驗證,但我只是回答具體問題 - 如果驗證程序將不匹配,則驗證程序仍然會失敗。