2016-12-04 134 views
0

編寫一個以二維整數數組爲參數的方法。該數組返回相同大小(行數和列數相同)的整數的新二維數組。返回數組中的值與參數數組中的值相同 EXCEPT中的值均爲負值。如何修復表達式編譯錯誤的非法開始?

我的方法不會編譯編譯器不斷抱怨我的方法,它的非法表達式的開始。它似乎沒有認識到我已經寫了一個方法,以至於什麼。

這是我做了什麼:

public class Examprep1 
{ 
    public static void main (String[] args) 
    { 
     int [][] array = new int[2][4]; 
     array[0][0]=1; 
     array[0][1]=5; 
     array[0][2]=-7; 
     array[0][3]=9; 
     array[1][0]=-2; 
     array[1][1]=4; 
     array[1][2]=6; 
     array[1][3]=-8; 

     int x; 

     // Using for loops to create the 2D array 
     for (int rows=0; rows<2; rows++) 
     { 
      for (int cols=0; cols<4;cols++) 
      { 
       System.out.print(array[rows][cols]+ " "); 
      } 
      System.out.println(""); 
     } 
     System.out.println(newArray(array)); 

     private static int[][] newArray(int[][] old) 
     { 
      int y; 
      int[][] current = new int [2][4]; 

      for (int rows=0; rows<2; rows++) 
      { 
       for (int cols=0; cols<4;cols++) 
       { 
        if (old[rows][cols]<0) 
        { 
         y=Math.abs(old[rows][cols]); 
         old[rows][cols] = (int)Math.pow(old[rows][cols],2)/x; 
        } 
        old[rows][cols] = current[rows][cols]; 
       } 
      } 
      return current; 
     } 
    } 
} 
+1

你丟失了'}''後的System.out.println(newArray(陣列));',結束'main'。在*之前,你需要這樣做*你可以聲明'newArray'。然後在'newArray'之後有一個額外的結束'}'。最後,在'old [rows] [cols] =(int)Math.pow(old [rows] [cols],2)/ x;'' –

+3

您在方法中定義了一個方法。你不能這樣做 –

+0

如果這個方法應該總是和輸入一樣的大小,你不應該依賴'current = new int [2] [4]'。這也是很不清楚,你爲什麼要平息數字。 –

回答

0

有很多編譯問題。你應該開始一步一步地寫代碼。

這裏的工作模式:你似乎有很大的過於複雜的實現(建議你參考)

class Examprep1 { 
    public static void main(String[] args) { 
     int[][] array = new int[2][4]; 
     array[0][0] = 1; 
     array[0][1] = 5; 
     array[0][2] = -7; 
     array[0][3] = 9; 
     array[1][0] = -2; 
     array[1][1] = 4; 
     array[1][2] = 6; 
     array[1][3] = -8; 

     int x = 0; 

     // Using for loops to create the 2D array 
     System.out.println("Printing old array"); 
     printArray(array); 

     System.out.println("Printing new array"); 
     printArray(newArray(array, x)); 

    } 

    private static void printArray(int[][] a) { 
     for (int rows = 0; rows < 2; rows++) { 
      for (int cols = 0; cols < 4; cols++) { 

       System.out.print(a[rows][cols] + " "); 
      } 
      System.out.println(""); 
     } 

    } 

    private static int[][] newArray(int[][] old, int x) { 
     int y; 
     int[][] current = new int[2][4]; 

     for (int rows = 0; rows < 2; rows++) { 
      for (int cols = 0; cols < 4; cols++) { 

       y = Math.abs(old[rows][cols]); 
       current[rows][cols] = y; 

      } 

     } 
     return current; 
    } 

} 
+0

哇它驚人的如何只是你花了一些時間來修復我的代碼。希望有一天我會一樣好......我有一種感覺,需要很長時間......感謝一大堆。我正在瀏覽你的代碼,看看我做錯了什麼。祝你週末愉快。 – theredfox24

+0

你對事物有很好的理解。只要在寫作地點放置東西。從一小段代碼開始。通過打印中間結果來保持調試。 – user1211

0

。您可以在一行中聲明和初始化2D數組。您也可以使用Arrays.deepToString(Object[])打印您的2D陣列。喜歡的東西,

public static void main(String[] args) { 
    int[][] array = { { 1, 5, -7, 9 }, { -2, 4, 6, -8 } }; 
    System.out.println(Arrays.deepToString(array)); 
    System.out.println(Arrays.deepToString(newArray(array))); 
} //<-- remember to close main. 

那麼你可以使用Object.clone()複製你的old陣列。迭代數組,並將當前值設置爲Math.abs(int)。像,

private static int[][] newArray(int[][] old) { 
    int[][] current = old.clone(); 
    for (int i = 0; i < current.length; i++) { 
     for (int j = 0; j < current[i].length; j++) { 
      current[i][j] = Math.abs(current[i][j]); 
     } 
    } 
    return current; 
} 

其輸出

[[1, 5, -7, 9], [-2, 4, 6, -8]] 
[[1, 5, 7, 9], [2, 4, 6, 8]] 
+0

謝謝Elliott提供的所有重要提示。我不知道old.clone()存在。我很確定我的教科書正試圖毀掉我的生活。每個過程都存在過於複雜。無論如何,你真的幫了忙。週末愉快。 – theredfox24