编写一个以二维整数数组为参数的方法。该数组返回相同大小(行数和列数相同)的整数的新二维数组。返回数组中的值与参数数组中的值相同 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]'。这也是很不清楚,你为什么要平息数字。 –