如何修复表达式编译错误的非法开始?
编写一个以二维整数数组为参数的方法。该数组返回相同大小(行数和列数相同)的整数的新二维数组。返回数组中的值与参数数组中的值相同 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;
}
}
}
有很多编译问题。你应该开始一步一步地写代码。
这里的工作模式:你似乎有很大的过于复杂的实现(建议你参考)
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;
}
}
哇它惊人的如何只是你花了一些时间来修复我的代码。希望有一天我会一样好......我有一种感觉,需要很长时间......感谢一大堆。我正在浏览你的代码,看看我做错了什么。祝你周末愉快。 – theredfox24
你对事物有很好的理解。只要在写作地点放置东西。从一小段代码开始。通过打印中间结果来保持调试。 – user1211
。您可以在一行中声明和初始化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]]
谢谢Elliott提供的所有重要提示。我不知道old.clone()存在。我很确定我的教科书正试图毁掉我的生活。每个过程都存在过于复杂。无论如何,你真的帮了忙。周末愉快。 – theredfox24
你丢失了'}''后的System.out.println(newArray(阵列));',结束'main'。在*之前,你需要这样做*你可以声明'newArray'。然后在'newArray'之后有一个额外的结束'}'。最后,在'old [rows] [cols] =(int)Math.pow(old [rows] [cols],2)/ x;'' –
您在方法中定义了一个方法。你不能这样做 –
如果这个方法应该总是和输入一样的大小,你不应该依赖'current = new int [2] [4]'。这也是很不清楚,你为什么要平息数字。 –