用菱形形式的数字填充二维数组 [英] Filling a 2d array with numbers in a rhombus form

查看:26
本文介绍了用菱形形式的数字填充二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来使用嵌套的 for 循环填充二维数组.所需的输出是这个...

I need some help filling a 2D array using a nested for loop. The desired output is this...

20.0 19.0 18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0
19.0 18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0
18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0
17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0
16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0
15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0
14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0
13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0
12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0
11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0
10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0
12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0
13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0
14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0
15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0
16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0
17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0
18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0
19.0 18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0
20.0 19.0 18.0 17.0 16.0 15.0 14.0 13.0 12.0 11.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0

这是我填充数组的代码...

This is my code to fill the array...

int row;
int column;
int counter = 10;
int counter1 = 10;
for (row = 0; row < array.length; row++) {
    array[row][0] = obj.getZ(10, counter);
    counter--;
    for (column = 0; column < array[row].length; column++) {
        array[row][column] = obj.getZ(counter1, 10);
        counter1--;

我已经将数组声明为 20 x 20 并且 obj.getZ 只是简单地调用这个方法...

I already declared the array to be 20 by 20 and obj.getZ is just simply calling this method...

public double getZ(double x, double y) {
    double z = (Math.abs(x) + Math.abs(y));
    return z;

数组是由 x 和 y 的绝对值组成的 z 值.

The array is the z value which is formed by the absolute values of x and y.

推荐答案

不是最好的解决方案,但应该可以解决问题:

Not the best solution, but it should solve the issue:

import java.util.Arrays;

class Scratch {
    public static void main(String[] args) {
        int[][] arr = new int[21][21];
        int row = 20;
        int column = 0;
        for (int i = 0; i < 21; i++) {
            column = row;
            for (int j = 0; j < 21; j++) {
                arr[i][j] = column;
                if (j < 10) {
                    column = column - 1;
                } else {
                    column = column + 1;
                }
            }
            if (i < 10) {
                row = row - 1;
            } else {
                row = row + 1;
            }
        }
        for (int[] arrRow : arr) {
            System.out.println(Arrays.toString(arrRow));
        }
    }
}

结果是:

[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

这篇关于用菱形形式的数字填充二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆