【问题标题】:How to create a 5x5 matrix with increasing numbers in java?如何在java中创建一个数字增加的5x5矩阵?
【发布时间】:2020-05-23 16:21:08
【问题描述】:

我正在尝试使用 for 循环创建一个 5x5 矩阵,但我做不到,我一直在寻找它一天但我仍然无法弄清楚,所以我放弃并现在寻求帮助. 我需要一个像这样的矩阵;

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

我做了一个充满零的矩阵;

 public class ExampleClass{ 
 public static void main(String args[]) { 



    int[][] Example =    new int [5][5];

        for(int i=0;i<Example.length;i++){
            for(int k=0;k<Example.length;k++)
            System.out.print(Example[i][k]+" ");
            System.out.println(); 

       }
    }
}
output:
           0  0  0  0  0  
           0  0  0  0  0  
           0  0  0  0  0  
           0  0  0  0  0  
           0  0  0  0  0  

【问题讨论】:

    标签: java arrays loops for-loop


    【解决方案1】:

    我会用另一个变量来做这件事,假设它叫做c

    int c = 1;
    for(int i=0;i<Example.length;i++){
      for(int k=0;k<Example.length;k++)
        if (k <= i) {
          System.out.print(c++ +" ");
         } else {
           System.out.print(0 +" ");   
         }
      System.out.println(); 
    }
    

    或者,如果您还想要适当的间距:

    int c = 1;
    for (int i = 0; i < Example.length; i++) {
        for (int k = 0; k < Example.length; k++) {
            if (k <= i) {
                if (c < 10) System.out.print(" ");
                System.out.print(" " + c++);
            } else {
                System.out.print("  " + 0);
            }
    
        }
        System.out.println();
    }
    

    这个输出:

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

    【讨论】:

    • 非常感谢您的回答。这个例子让我发疯了,你刚刚挽救了我的心理健康哈哈。
    • 这只会打印模式,但不会初始化Example[][]
    • 在问题之前添加int[][] Example = new int [5][5]; :)
    • @Balastrong - 你还不在那里。语句int[][] Example = new int [5][5]; 将使用默认值0 初始化数组;不是您打印的值。如果您想稍后在程序中使用该数组,所有索引都将具有0,而不是您打印的值。
    【解决方案2】:

    按如下方式进行:

    public class Main {
        public static void main(String[] args) {
            int[][] example = new int[5][5];
            int x = 1;
    
            // Initialize
            for (int i = 0; i < example.length; i++) {
                for (int k = 0; k <= i && k < example[i].length; k++) {
                    example[i][k] = x++;
                }
            }
    
            // Display
            for (int i = 0; i < example.length; i++) {
                for (int k = 0; k < example[i].length; k++) {
                    System.out.printf("%3d", example[i][k]);
                }
                System.out.println();
            }
        }
    }
    

    输出:

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

    另外,请始终关注Java naming conventions,例如变量Example 应命名为example

    【讨论】:

      【解决方案3】:

      这些值低于主对角线,包括它。所有这些值都在i 小于或等于j 的位置上,其中i 是行索引,j 是下面示例中的列索引。

      public static void main( String[] args ) {
      
          int[][] m = new int[5][5];
          int count = 1;
      
          for ( int i = 0; i < m.length; i++ ) {
              for ( int j = 0; j < m.length; j++ ) {
                  if ( j <= i ) {
                      m[i][j] = count++;
                  } else {
                      m[i][j] = 0;
                  }
              }
          }
      
          for ( int i = 0; i < m.length; i++ ) {
              for ( int j = 0; j < m.length; j++ ) {
                  System.out.printf( "%2d ", m[i][j] );
              }
              System.out.println();
          }
      
      }
      

      如果您只需要打印值,您可以执行以下操作:

      public static void main( String[] args ) {
      
          int size = 5;
          int count = 1;
      
          for ( int i = 0; i < size; i++ ) {
              for ( int j = 0; j < size; j++ ) {
                  if ( j <= i ) {
                      System.out.printf( "%2d ", count++ );
                  } else {
                      System.out.printf( "%2d ", 0 );
                  }
              }
              System.out.println();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-09
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        • 1970-01-01
        相关资源
        最近更新 更多