简体   繁体   English

试图在 Java 中使用数组,但不知道我做错了什么

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

I am trying to achieve the following structure,我正在尝试实现以下结构,

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

I tried with the following code but I can't make it work.我尝试使用以下代码,但无法使其正常工作。

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();
        }
    }
    }

This is the output i am getting for this.这是我为此得到的输出。

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

I really appreciate if any one can point me in the right direction.如果有人能指出我正确的方向,我真的很感激。 thanks in advance.提前致谢。

You want to change your column into 8 and row into 2 because your output value is 8 * 2 = 16 .您想将列更改为8并将行更改为2因为您的输出值为8 * 2 = 16 Your equation is doublearr[0][0] * (j + 1)你的方程是doublearr[0][0] * (j + 1)

If your equation is doublearr[0][0] * (j + 1) :如果你的方程是doublearr[0][0] * (j + 1)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

If equation change to doublearr[0][0] * (j + 1) * (i + 1) :如果方程更改为doublearr[0][0] * (j + 1) * (i + 1)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Here down is my code:下面是我的代码:

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();
    }
}
}

If you want to print double of the above column then you will need to do a small change:如果你想打印上列的两倍,那么你需要做一个小的改变:

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有时我不知道某些方法属于哪些类。 我究竟做错了什么? - Sometimes I don't know which classes certain methods belong to. What am I doing wrong? 我正在用Java中的“ 0”替换Evens,我在做什么错? - I am trying to replaceEvens with a “0” in java, what am I doing wrong? 如何在Java中跳过行的一部分-不知道我在做什么 - How to skip a portion of a line in Java- Don't know what I am doing 有人知道我在执行这个 java 代码时做错了什么吗? - Anyone know what I am doing wrong to execute this java code? 试图在Java中测试矩形的交集,我做错了什么? - Trying to test rectangles for intersection in Java, what am I doing wrong? 使用数组我在做什么错? - What am I doing wrong with using arrays? 我在方法中的数组做错了什么? - What am i doing wrong with arrays in methods? Java:Paint应用程序,代码不起作用,我在做什么错? - Java: Paint application, the code doesn't work, what am I doing wrong? 官方 Java 练习解决方案不起作用……我做错了什么? - Official Java excercize solution doesn't work… what I am doing wrong? 我的手电筒应用程序运行缓慢并且崩溃,我不知道自己在做什么错 - My flashlight app is slow and crashes allot i don't know what I'm doing wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM