[英]Populate a 2D Array with ints
Title is slightly confusing... Sorry. 标题有点令人困惑...对不起。
I have to answer a question, and I started it, but I'm not 100% sure of what is being asked. 我必须回答一个问题,然后我开始了,但是我不确定要问什么。 Perhaps one of you understand it.
也许你们中的一员了解这一点。
Here is the question: 这是问题:
Write the code for populating a 2-D array of ints that models a multiplication table. 编写代码,以填充对乘法表建模的int二维数组。 The array should have 12 rows and 12 columns.
该数组应具有12行和12列。 Each entry in the 'table' should be the product of the row*col -- eg The element at arr[2][3] = 6 and the element at arr[0][11] = 0.
“表”中的每个条目应为row * col的乘积-例如,arr [2] [3] = 6处的元素和arr [0] [11] = 0处的元素。
Here is what I have so far, but I don't know how to continue: 这是我到目前为止的内容,但我不知道如何继续:
int arr[][] = new int[12][12];
int mult;
for(int row = 0; row < arr.length; row++){
for(int col = 0; col < arr[1].length; col++){
}
}
basically a 2d array can store value (for a tutorial click here ). 基本上,二维数组可以存储值(有关教程,请单击此处 )。 What you want to do is save the value in the block it self.
您要做的就是将值保存在自身的块中。
int arr[][] = new int[12][12];
for(int row = 0; row < arr.length; row++){
for(int col = 0; col < arr[1].length; col++){
arr[row][col]=row*col;
}
}
Then use a double for loop to see the output like: 然后使用double for循环查看输出,如下所示:
for(int row = 0; row < arr.length; row++){
for(int col = 0; col < arr[1].length; col++){
System.out.print(row +" * " col + " = "+arr[row][col]+" ");
}
System.out.println();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.