尝试使用 java 中的数组,但不知道我做错了什么

trying to work with arrays in java but don't know what i am doing wrong

我正在尝试实现以下结构,

100    200    300    400    500    600    700    800
200    400    600    800  1,000  1,200  1,400  1,600

我尝试了以下代码,但无法正常工作。

public class Main {

    public static void main(String[] args) {
        int cols = 5;
        int rows = 4;
        int number = 100;
        int [][] doublearr = new int [rows][cols];


        for (int i = 0; i < doublearr.length ;i ++){
            for(int j=0; j < doublearr[i].length; j++){
                    doublearr[0][0] = number;
                    doublearr[i][j] = doublearr[0][0] *( j+1);
                    System.out.print("\t" + doublearr[i][j]);
            }
            System.out.println();
        }
    }
    }

这是我为此得到的输出。

100 200 300 400 500
100 200 300 400 500
100 200 300 400 500
100 200 300 400 500

如果有人能指出正确的方向,我将不胜感激。提前致谢。

您想将您的列更改为 8,将行更改为 2,因为您的输出值为 8 * 2 = 16。你的等式是 doublearr[0][0] * (j + 1)

如果你的等式是doublearr[0][0] * (j + 1):

1. 100 * (0 + 1) = 100

2. 100 * (1 + 1) = 200

3. 100 * (2 + 1) = 300

4. 100 * (3 + 1) = 400

5. 100 * (4 + 1) = 500

6. 100 * (5 + 1) = 600

7. 100 * (6 + 1) = 700

8. 100 * (7 + 1) = 800

9. 100 * (0 + 1) = 100

10. 100 * (1 + 1) = 200

11. 100 * (2 + 1) = 300

12. 100 * (3 + 1) = 400

13. 100 * (4 + 1) = 500

14. 100 * (5 + 1) = 600

15. 100 * (6 + 1) = 700

16. 100 * (7 + 1) = 800

如果等式变为doublearr[0][0] * (j + 1) * (i + 1)

1. 100 * (0 + 1) * (0 + 1)= 100

2. 100 * (1 + 1) * (0 + 1)= 200

3. 100 * (2 + 1) * (0 + 1)= 300

4. 100 * (3 + 1) * (0 + 1)= 400

5. 100 * (4 + 1) * (0 + 1)= 500

6. 100 * (5 + 1) * (0 + 1)= 600

7. 100 * (6 + 1) * (0 + 1)= 700

8. 100 * (7 + 1) * (0 + 1)= 800

9. 100 * (0 + 1) * (1 + 1)= 200

10. 100 * (1 + 1) * (1 + 1)= 400

11. 100 * (2 + 1) * (1 + 1)= 600

12. 100 * (3 + 1) * (1 + 1)= 800

13. 100 * (4 + 1) * (1 + 1)= 1000

14. 100 * (5 + 1) * (1 + 1)= 1200

15. 100 * (6 + 1) * (1 + 1)= 1400

16. 100 * (7 + 1) * (1 + 1)= 1600

下面是我的代码:

public class MyClass {
    public static void main(String[] args) {
    int cols = 8;
    int rows = 2;
    int number = 100;
    int [][] doublearr = new int [rows][cols];


    for (int i = 0; i < doublearr.length ;i ++){
        for(int j=0; j < doublearr[i].length; j++){
                doublearr[0][0] = number;
                doublearr[i][j] = doublearr[0][0] * (j + 1) * (i + 1);
                System.out.print(" " + doublearr[i][j]);
        }
        System.out.println();
    }
}
}

如果你想打印以上列的两倍,那么你需要做一个小的改变:

for (int i = 0; i < doublearr.length ;i ++){
    for(int j=0; j < doublearr[i].length; j++){
            doublearr[0][0] = number;
            doublearr[i][j] = doublearr[0][0] *( j+1);
            System.out.print("\t" + doublearr[i][j]);
    }
  number*=2;  // add this line
    System.out.println();
}