有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何创建具有用户定义维度的矩阵,并使用从上到下、从左到右的递增值填充它?

我正在练习Java,我得到了这个,我不知道在哪里以及如何继续:

  • Creates a matrix with user-defined dimensions.
  • Fills the matrix with increasing values from top to bottom, left to right.
  • Print the status of the matrix.

我必须达到的结果应该是这样的5x5

0  5  10  15  20
1  6  11  16  21
2  7  12  17  22
3  8  13  18  23
4  9  14  19  24

或者这个7x2

0  7
1  8
2  9
3  10
4  11
5  12
6  13

这就是我所做的:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int count = 0;

    System.out.print("Insert rows [i] : ");
    int i = in.nextInt();
    in.nextLine();

    System.out.println("Insert columns [j] : ");
    int j = in.nextInt();
    in.nextLine();

    int[][] matrix = new int[i][j];
    for (int k = 0; k < matrix.length; k++) {
        for (int l = 0; l < matrix[i].length; l++) {
            matrix[i][j] = count;
            count++;
        }
    }
}

共 (2) 个答案

  1. # 1 楼答案

    如果你理解nested loops是如何工作的,这并不困难。在内部循环中,可以从等于行索引的值开始,并在内部循环的每次迭代中按行数递增该值

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
    
            System.out.print("Enter the number of rows: ");
            int rows = in.nextInt();
    
            System.out.print("Enter the number of columns: ");
            int cols = in.nextInt();
    
            int[][] matrix = new int[rows][cols];
    
            // Fill the array
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0, value = i; j < matrix[i].length; j++, value += rows) {
                    matrix[i][j] = value;
                }
            }
    
            // Display the matrix
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[i].length; j++) {
                    System.out.print(matrix[i][j] + "\t");
                }
                System.out.println();
            }
        }
    }
    

    运行示例:

    Enter the number of rows: 5
    Enter the number of columns: 5
    0   5   10  15  20  
    1   6   11  16  21  
    2   7   12  17  22  
    3   8   13  18  23  
    4   9   14  19  24  
    

    另一个示例运行:

    Enter the number of rows: 7
    Enter the number of columns: 2
    0   7   
    1   8   
    2   9   
    3   10  
    4   11  
    5   12  
    6   13  
    
  2. # 2 楼答案

    您可以使用流中的流来填充这样的数组:

    int m = 5;
    int n = 4;
    int[][] arr = new int[m][n];
    IntStream.range(0, m).forEach(i ->
            IntStream.range(0, n).forEach(j ->
                    arr[i][j] = i + j * m));
    
    Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
    //[0, 5, 10, 15]
    //[1, 6, 11, 16]
    //[2, 7, 12, 17]
    //[3, 8, 13, 18]
    //[4, 9, 14, 19]
    

    或者您可以在循环中使用循环

    int m = 7;
    int n = 2;
    int[][] arr = new int[m][n];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            arr[i][j] = i + j * m;
        }
    }
    
    for (int[] row : arr) {
        System.out.println(Arrays.toString(row));
    }
    //[0, 7]
    //[1, 8]
    //[2, 9]
    //[3, 10]
    //[4, 11]
    //[5, 12]
    //[6, 13]
    

    另见:Printing a snake pattern using an array