2015-11-03 94 views
0

我有問題讓我的類編譯。它給我錯誤,如「雙不能取消」和「找不到符號」。使用數組作爲方法(Java)

的潛水員是:

public class MinilabArraysDriver{ 
    public static void main(String[ ] args){ 
     //create a 1d array of ints 
     int[ ] array1d = { 8, 8, 7, 5, 3 }; 

     //call mean1d Method to find the mean (note the static call...) 
     double theMean = MinilabArrays.mean1d(array1d); 

     //print the original array and the mean 
     System.out.print("The mean of: { "); 
     for (int i=0; i<array1d.length; i++){ 
       if (i != 0) 
        System.out.print(" "); 
      //print first for separation (except before first element) 
      System.out.print(array1d[i]); 
     } 
     System.out.print(" } is: " + theMean); 
     //------------------------------------------------------------ 
     System.out.println("\n\n"); 
     //create a 2d array of doubles 
     double[ ][ ] array2d = {{ 3.4, 5.1, 8.0}, 
           { 5.23, 8.2 }, 
           { 10.7 }, 
           { 2.9 } 
           }; 
     //call sum2d to get the sum 
     double total = MinilabArrays.sum2d(array2d); 

     //print the 2D array 
     for (int row=0; row<array2d.length; row++){ 
      System.out.println(); 
      for (int col=0; col<array2d[row].length; col++) 
        System.out.print(array2d[row][col] + "\t"); 
      } 
     //print the result 
     System.out.println("\n\nTotal of 2d array is: " + total); 
     System.out.println("\n\n"); 
     } 
} 

這裏是我想寫類:

public class MinilabArrays{ 
    public static double mean1d(int[ ] theMean){ 
    double total = 0; 
    for (int i = 0 ;i < theMean.length ; i++){ 
      total = total + theMean [ i ]; 
      total = total/theMean.length; 
      return total; 
     } 
    } 

    public static double sum2d(double [ ] theSum){ 
    double total2, total3, total4 = 0; 
    for(int row=0 ; row < theSum.length ; row++){ 
     for (int col=0 ; col<theSum[row].length ; col++) 
      total2 = total2 + theSum[col]; 
     } 
    total3 = total3 + theSum[row]; 
    total4 = total3 + total2; 
    return total4; 
    } 
} 

對於「mean1d」我試圖找到給定的均值或平均數量,併爲「sum2d」我試圖找到給定數量的總和。

對不起,如果我的代碼是愚蠢的或愚蠢的。如果它不讓我編譯,我無法檢查它。

謝謝你幫助我!

+0

究竟是什麼錯誤,你好嗎? –

+0

當你得到它來編譯時,你會發現'mean1d'中的循環並不是一個循環。它只會執行一次然後離開。 – ajb

+0

我得到「雙不能被解除引用」和「找不到符號」。 – Paincakes

回答

1

您正在通過在參數中傳遞2D數組來調用sum2d方法,並將此方法聲明爲一維數組作爲參數。

MinilabArrays.java

public static double sum2d(double[][] theSum) { 
    double total2 = 0; 
    for (int row = 0; row < theSum.length; row++) { 
     for (int col = 0; col < theSum[row].length; col++){ 
      total2 = total2 + theSum[row][col]; 
     } 
    } 
    return total2; 
} 
+0

Got it!感謝大家的幫助。面向對象的編程對我來說很困難。 – Paincakes

1

第一個錯誤來自此行for (int col=0 ; col<theSum[row].length ; col++)。您正在使用二維數組theSum[][],但將一維數組聲明爲方法參數。

另一個錯誤來自使用範圍之外的row。你在這個for循環中聲明它for(int row=0 ; row < theSum.length ; row++),但在for循環之外使用它。

+0

我該如何解決?數組中的數字本身是double,但col和row是int。 – Paincakes

+0

@Paincakes比較是根本不可能的。你必須重新考慮你在做什麼。 – Alexander

1

MinilabArrays.sum2d(array2d)需要雙[]不是雙[] []這裏。 而且在這一點上total3 = total3 + theSum [row];無法訪問for循環外部的變量

+0

我只是把「total3 = total3 + theSum [row];」在for循環中。 \t \t { \t \t \t對(INT COL = 0; COL Paincakes