如何显示在Java中的N×随机数的N矩阵?

问题描述 投票:2回答:2

我试图创建随机双数字矩阵。矩阵必须是大小为n×n的和所有数字必须在1和100之间。我一直在努力,现在梳理出来的年龄,我知道那一定是这么简单的东西(因为它通常是这样)。

这里是我的代码:

public static void main(String[] args) { 
    PrintRandomGraph(RandomArray(4));
}

private static double[] RandomArray(int n) {  
    double[] randomArray = new double[n];
    double[][] randomMatrix = new double [n][n];

    Random rand = new Random(); 
    rand.setSeed(System.currentTimeMillis()); 
    for (int i = 0; i < n; i++) {

        Integer r = rand.nextInt()% 100; 
        randomArray[i] = Math.abs(r);
        for (int j = 0; j < n; j++) {
            Arrays.fill(randomMatrix, i, i+1, randomArray);
        }  
    }

    return randomArray;  
}

private static void PrintRandomGraph(double[] inputArray) {
    int n = inputArray.length;
    double[] showArray = new double[n];
    double[][] showMatrix = new double [n][n];

    for (int j = 0; j < n; j++) {
        for (int i = 0; i < n; i++) {
            double r = inputArray[i];
            showArray[i] = r; 
            Arrays.fill(showMatrix, i, i+1, showArray);
        }
    }

    System.out.println(Arrays.deepToString(showMatrix));
}

当我运行的代码,我得到一个随机排列重复n次,如:

[[63.0, 97.0, 64.0, 75.0], [63.0, 97.0, 64.0, 75.0], [63.0, 97.0, 64.0, 75.0], [63.0, 97.0, 64.0, 75.0]]

我想我需要回去的顶部for循环,并添加新的数组...?请帮助=(

任何帮助深表感谢。谢谢。

java arrays matrix for-loop
2个回答
3
投票

您是否尝试过这样的事情:

private static double[][] RandomArray(int n) {
    double[][] randomMatrix = new double [n][n];

    Random rand = new Random(); 
    rand.setSeed(System.currentTimeMillis()); 
    for (int i = 0; i < n; i++) {     
        for (int j = 0; j < n; j++) {
            Integer r = rand.nextInt()% 100; 
            randomMatrix[i][j] = Math.abs(r);
        }

    }

    return randomMatrix;
}

打印图形:

public void printGraph(double[][] array, int n) {
    for (int i = 0; i < n; i++) {     
        for (int j = 0; j < n; j++) {
            System.out.println(array[i][j]);
        } 
    }
}

为了用方括号打印图表:

public void printGraph(double[][] array, int n) {
        for (int i = 0; i < n; i++) {  
            System.out.print(" [ ");   
            for (int j = 0; j < n; j++) {
                System.out.print(array[i][j]);
            } 
            //Put println instead of print here to have each row in a new line
            System.out.print(" ]");
        }
    }

此外,这似乎是一个很像功课,如果是,请标记它像这样:)


0
投票

你好,这一切提供了我一直在寻找的答案

 private static double[][] RandomArray(int n) {
        double[][] randomMatrix = new double [n][n];
        double[] randomArray = new double [n];
         Random rand = new Random(); 
            rand.setSeed(System.currentTimeMillis()); 

        for (int i = 0; i < n; i++) {   

            for (int j = 0; j < n; j++) {
                Integer r = rand.nextInt()% 100; 
                randomMatrix[i][j] = Math.abs(r);
            }

        }

        return randomMatrix;
    }


public static void main(String[] args){

        //PrintRandomGraph(RandomArray(5));
    System.out.println(Arrays.deepToString(RandomArray(5)));
}
© www.soinside.com 2019 - 2024. All rights reserved.