編寫一個以二維整數數組爲參數的方法。該數組返回相同大小(行數和列數相同)的整數的新二維數組。返回數組中的值與參數數組中的值相同 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;
}
}
}
你丟失了'}''後的System.out.println(newArray(陣列));',結束'main'。在*之前,你需要這樣做*你可以聲明'newArray'。然後在'newArray'之後有一個額外的結束'}'。最後,在'old [rows] [cols] =(int)Math.pow(old [rows] [cols],2)/ x;'' –
您在方法中定義了一個方法。你不能這樣做 –
如果這個方法應該總是和輸入一樣的大小,你不應該依賴'current = new int [2] [4]'。這也是很不清楚,你爲什麼要平息數字。 –