doTheMath(int [] [],int [] [],char) 将两个二维整数数组作为参数,并返回一个二维整数数组。它的第三个参数是一个字符,它告诉方法要执行哪个算术运算。它将创建一个与通过参数进入的数组大小相同的新数组。它将使用character参数来确定填充新数组时要执行的操作。例如,如果字符参数包含' +'符号,此方法将在每个数组中添加相应的条目,并将它们的总和放在新数组中的相同位置。它应该处理4个操作:添加(' +'),减去(' - '),乘以(' *')和除(') 39 /&#39)。如果有任何其他字符,它应该对两个数组的内容执行余数(模数)操作。
因此我目前拥有的是我的代码...
public static int [][]doTheMath(int [][]array1, int [][]array2, char arithmetic)
{
// Declares 2-dimensional array the same size as one in parameters
int [][]arraySum = new int [array1.length][array2.length];
// For loop to take each row and column to separately do arithmetic on
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1.length; j++) {
if (arithmetic == '+')
arraySum [i][j] = array1 [i][j] + array2 [i][j];
else if (arithmetic == '-')
arraySum [i][j] = array1 [i][j] - array2 [i][j];
else if (arithmetic == '*')
arraySum [i][j] = array1 [i][j] * array2 [i][j];
else if (arithmetic == '/')
arraySum [i][j] = array1 [i][j] / array2 [i][j];
else
arraySum [i][j] = array1 [i][j] % array2 [i][j];
}
}
// arraySum value is returned
return arraySum;
}
要在我的主要方法中调用这个方法会是正确的吗?
// 2-dimensional array created from values taken from doTheMath() method class
int [][]sum = doTheMath(array1,array2, '+');
// For loop to print the 2-dimensional array results after arithmetic
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.printf("%4s", sum[i][j] + " ");
}
System.out.println(" ");
}
我从这样打印得到的值似乎给了我错误的价值。
9 7 2 3 2 5 16 5 7
3 3 3 + 4 1 8 = 6 7 9
4 5 8 2 3 7 9 10 10
Array1和Array 2
// Declared arrays
int [][] array1;
int [][] array2;
// Calls getSize() to get the number for rows and then columns
int rows = getSize("Please enter number of rows: ");
int columns = getSize("Please enter number of columns: ");
// Instantiate input for rows and columns into both double arrays
array1 = new int [rows][columns];
array2 = new int [rows][columns];
// For loop to fill in the rows and column values with random integers
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array1 [i][j] = (int)(Math.random() * 9 + 1);
array2 [i][j] = (int)(Math.random() * 9 + 1);
System.out.printf("%4s", array1[i][j] + " ");
System.out.printf("%4s", array2[i][j] + " ");
}
System.out.println(" ");
}
答案 0 :(得分:0)
所以我发现如果我拿这个我认为可以用来并排打印矩阵。
// For loop to fill in the rows and column values with random integers
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array1 [i][j] = (int)(Math.random() * 9 + 1);
array2 [i][j] = (int)(Math.random() * 9 + 1);
System.out.printf("%4s", array1[i][j] + " ");
System.out.printf("%4s", array2[i][j] + " ");
}
System.out.println(" ");
}
将其更改为两个单独的循环,为我提供了正确的结果。
// For loop to fill in the rows and column values with random integers
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array1 [i][j] = (int)(Math.random() * 9 + 1);
System.out.printf("%4s", array1[i][j] + " ");
}
System.out.println(" ");
}
// For loop to fill in the rows and column values with random integers
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array2 [i][j] = (int)(Math.random() * 9 + 1);
System.out.printf("%4s", array2[i][j] + " ");
}
System.out.println(" ");
}