【问题标题】:How to write BOTH row and column indexes in a table (java) - nested for loops如何在表中编写行和列索引(java)-嵌套的for循环
【发布时间】:2014-01-03 14:32:22
【问题描述】:

我想输出显示每个单元格的行和列索引 [i][j] 的表格,

我只能打印 i-index 但不知道如何打印“列”索引

它应该是这样的: 对于每一列 - 上面显示了索引 - 第一条水平线 - 0 1 2 3 4 5 6 7 8 9 10

我的代码:

public static void print_table( int table[][] ){
    System.out.println( "PRINTING TABLE ") ;

    for(int i = 0; i <= NCHANGES; i++) {
        System.out.print("[" + i + "]") ;// this prints i-index for EVERY ROW
        for(int j = 0; j <= MAX_AMOUNT; j++) {
            System.out.print(table[i][j] + "\t") ;
            // BUT HOW TO PRINT J(COLUMN) INDEX for every column?
        }
        System.out.println() ;
    }
}

我相信这对于所有需要打印出他们的 2 嵌套循环的结果的人来说都是有用的

【问题讨论】:

    标签: java for-loop output nested-loops


    【解决方案1】:

    在执行嵌套循环之前,只需创建一个 for 循环并打印
    - I/J 单元
    - 列标题单元格(0、1、2 等)
    - 新行

    然后做你已经在做的事情。

    【讨论】:

      【解决方案2】:

      大概是这样的吧?

          public static void main(String[] args) {
          int[][] twoD = new int[][] { { 123, 456, 789 }, { 123, 456, 789 },
                  { 123, 456, 789 } };
      
          StringBuilder sb = new StringBuilder();
      
          for (int I = 0; I < twoD.length; I++) {
              for (int X = 0; X < twoD[I].length; X++) {
                  sb.append("Row " + I);
                  sb.append(" Column " + X);
                  sb.append(" Value " + twoD[I][X]);
                  sb.append("\n");
                  System.out.print(sb.toString());
                  sb.setLength(0);
              }
          }
      }
      

      控制台输出:

      Row 0 Column 0 Value 123
      Row 0 Column 1 Value 456
      Row 0 Column 2 Value 789
      Row 1 Column 0 Value 123
      Row 1 Column 1 Value 456
      Row 1 Column 2 Value 789
      Row 2 Column 0 Value 123
      Row 2 Column 1 Value 456
      Row 2 Column 2 Value 789
      

      【讨论】:

      • 嗯,不完全是 - 但感谢您查看我的问题!
      • 查找我的答案 - 这就是我想要的 - 我完成了!
      【解决方案3】:

      也许是这个?

      public static void print_table( int table[][] ){
       System.out.println( "PRINTING TABLE ") ;
      
       //Extra loop here
       System.out.print("I/J ");
       for (int j = 0; j <= MAX_AMOUNT; j++){
           System.out.print(j + " ");  
       }
      
       for(int i = 0 ; i <= NCHANGES ;i++){
      
         System.out.print("[" + i + "]") ;// this prints i-index for EVERY ROW
      
               for(int j = 0 ; j <= MAX_AMOUNT  ; j++){
      
            System.out.print(table[i][j] + "\t") ;// BUT HOW TO PRINT J(COLUMN) INDEX for every column?
      
                       }
                       System.out.println() ;
               }
      
       }
      

      【讨论】:

        【解决方案4】:
        public static void print_table( int table[][] ){
         System.out.println( "\n\nPRINTING TABLE ") ;
        
         System.out.println("\t\tm") ;
          System.out.print("\n        ") ;
         for(int column = 0 ; column <= MAX_AMOUNT; column++){
                   System.out.print(  column  + " \t"  ) ;
         }
         System.out.print("\n----------------------------------------------------------------------------------------------------------------------------------") ;
         System.out.println("----------------------------------------------------------") ;
         for(int i = 0 ; i <= NCHANGES ;i++){
           System.out.print("[" + i + "]:  ") ;
                 for(int j = 0 ; j <= MAX_AMOUNT  ; j++){
        
                            System.out.print(table[i][j] + "\t") ;
        
                         }
                         System.out.println() ;
                 }
        
         }
        

        正是我想要的!

        【讨论】:

        • 如果这里的答案之一帮助您解决了它,请接受答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-15
        • 1970-01-01
        • 2022-06-15
        • 2021-03-13
        • 2023-04-06
        相关资源
        最近更新 更多