英文:
How can I use a for loop to create a two dimensional array and assign linear values with an increment of 10?
问题
我想使用for循环创建一个二维数组,并希望以10的增量分配值。到目前为止,这是我的代码,但我没有得到想要的结果...
int rows = 3;
int columns = 3;
int[][] array = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = j * 10;
System.out.println(array[i][j]);
}
}
这是我希望的输出:
0 30 60
10 40 70
20 50 80
Process finished with exit code 0
这是我一直得到的输出:
0
10
20
0
10
20
0
10
20
Process finished with exit code 0
英文:
I want to create a two dimensional array using a for loop and I want to assign values with an increment of 10. So far here is what I have and I am not getting the result I want...
package ~/TwoDimensionalArray;
import java.util.Scanner;
public class TwoDimensionalArray {
public static void main(String[] args) {
int rows = 3;
int columns = 3;
int[][] array = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = j * 10;
System.out.println(array[i][j]);
}
}
}
}
Here is what I want my output to be:
0 30 60
10 40 70
20 50 80
Process finished with exit code 0
Here is what I keep getting:
0
10
20
0
10
20
0
10
20
Process finished with exit code 0
答案1
得分: 1
- 每个数字后不应该打印新行。
- 只有在每行被打印后才应该打印新行,也就是在外部循环的每次迭代后。
- 每个数字后面也应该打印一个制表符,以防止数字粘在一起。
- 你可以使用公式
j * 10 * rows + i * 10
计算第 i 行第 j 列的数字,这样可以得到每个位置上的正确数字。
英文:
- You should not print a new line after each number.
- You should only print a new line after each row is printed, that is, after each iteration of the outer loop.
- You should also print a tab character after each number so that the numbers don't stick together
- You should calculate the number at row i column j using the formula
j * 10 * rows + i * 10
. This gives you the correct number at each position.
int rows = 3;
int columns = 3;
int[][] array = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = j * 10 * rows + i * 10;
System.out.print(array[i][j]); // "print" doesn't print a new line
System.out.print("\t"); // print a tab!
}
System.out.println(); // print a new line here
}
答案2
得分: 0
以下是翻译好的部分:
public class TempTest {
static final int ROW = 3;
static final int COLUMN = 3;
public static void main(String[] args) {
int[][] array = new int[ROW][COLUMN];
// 为数组分配值
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
array[i][j] = 10 * (i + 3 * j);
}
}
// 显示数组
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
System.out.print(array[i][j]);
System.out.print(' ');
}
System.out.println();
}
}
}
和结果将会是
0 30 60
10 40 70
20 50 80
我认为您可能不熟悉print
和println
之间的区别,后者会添加一个新行。尝试学更多关于JAVA的知识,祝好运!
英文:
maybe this is what u want
public class TempTest {
static final int ROW = 3;
static final int COLUMN = 3;
public static void main(String[] args) {
int[][] array = new int[ROW][COLUMN];
// assign values to array
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
array[i][j] = 10 * (i + 3 * j);
}
}
// display array
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
System.out.print(array[i][j]);
System.out.print(' ');
}
System.out.println();
}
}
}
and the result will be
0 30 60
10 40 70
20 50 80
I think u are not familiar with the difference between print
and println
, the latter one will add a new line. Try learn more about JAVA, good luck!
答案3
得分: 0
尝试这个。
int rows = 3;
int columns = 3;
int[][] array = new int[rows][columns];
for (int i = 0, v = 0; i < rows; i++) {
for (int j = 0; j < columns; j++, v += 10) {
array[i][j] = v;
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
英文:
Try this.
int rows = 3;
int columns = 3;
int[][] array = new int[rows][columns];
for (int i = 0, v = 0; i < rows; i++) {
for (int j = 0; j < columns; j++, v += 10) {
array[i][j] = v;
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论