【发布时间】:2017-11-06 03:47:29
【问题描述】:
以下代码是我到目前为止所拥有的,根本没有任何效果。我打算主要声明一个 7×10 的二维数组。将该数组传递给使用随机整数 [1,9] 填充数组的方法。将填充的数组传递给一个方法,该方法打印二维数组,其中每行末尾的行总和以及每列末尾的列总和。还必须打印行和列标签。任何帮助将不胜感激。
package twodimensionalarray;
import java.util.Arrays;
import java.util.Random;
/**
*
* @author Kristy7
*/
public class TwoDimensionalArray {
//This method is for generating random integers between 1 and 9.
public static int randInt(){
Random rand = new Random();
int randNum;
randNum = rand.nextInt(9)+1;
return randNum;
}
//This method is for filling a 2D array with random integers
public static int[][] fillArray(int[][] toFill){
int[][] toReturn = new int[0][0];
for(int r = 0; r < toFill.length; r++){
for(int c = 0; c < toFill[0].length; c++){
toReturn[toFill[r]][toFill[c]] = randInt(toFill);
}
}
return toReturn;
}
//This method is for for summing rows and columns, and printing rows, columns, the sum of ruws at each row, and the sum of columns at each column.
public static void summingRC(int[][] toSum){
int[] colSums = new int[0];
for(int i = 0; i < toSum.length; i++){
System.out.print(i);
}
System.out.println();
int sum = 0;
for (int r = 0; r < toSum.length; r++) {
for (int c = 0; c < toSum[r].length; c++) {
sum += toSum[c][r];
colSums[c] += toSum[r][c];
}
System.out.println(sum);
}
System.out.println(colSums);
}
/**
* @param args the command line arguments
*
*/
public static void main(String[] args) {
//Declare a 7x10 2D array
int [][] twoDArray = new int[7][10];
//call fillArray method.
int[][] fillingArray = fillArray(twoDArray);
//call SummingRC method
summingRC(fillingArray);
}
}
【问题讨论】:
-
提交代码时,请将其粘贴,用鼠标选中,然后在编辑器中按下
{}按钮。这将正确格式化它。