java - 打印矩阵的对角线并将其显示在屏幕上,以便每个值准确显示其应在的位置

标签 java loops matrix

我编写了一些代码来处理两个级别的嵌入式循环。在主要方法结束之前,我对代码的最后一部分有问题,我只想在对角线上打印元素。该代码打印了值,但不是我想要的方式。当我们只写矩阵的 digonlas 时,我陷入了要给出的选项卡数量的困境,以便以在纸上显示的方式打印该值。

这是我的代码:

package com.codopedia.java7.sep2014;

/**
 *
 * @author www.codopedia.com
 */
public class TwoDArrayExp1 {

    public static void main(String args[]) {
        int row = 5, column = 5, k = 0;
        int my2dArray1[][] = new int[row][column];//5 rows and 5 columns
        //Initializing the array elements to zero

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                my2dArray1[i][j] = k;
                k++;
            }
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print("(" + (i + 1) + " , " + (j + 1) + ")" + " = " + " " + my2dArray1[i][j] + "\t");
            }
            System.out.println();//För att börja en ny rad
        }
        System.out.println();
        System.out.println();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (j > i) {//When column is greater than row (Printing on the digagonal and below it only.)
                    continue;//Stop printing at this row and go to the next row
                }
                //System.out.print("(" + (i + 1) + " , " + (j + 1) + ")" + " = " + " " + my2dArray1[i][j] + "\t");
                System.out.print(my2dArray1[i][j] + "\t");
            }
            System.out.println();//För att börja nya rad.
        }
        System.out.println();
        System.out.println();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (j >= i) {  //Priniting on the diagonal and above it only.                  
                    System.out.print(my2dArray1[i][j] + "\t");
                }
            }
            System.out.println();//För att börja nya rad.
            for (int x = 0; x <= i; x++) {
                System.out.print("\t");
            }
        }
        System.out.println();
        System.out.println();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (i == j) {
                    System.out.print(my2dArray1[i][j]);
                }

            }
            System.out.println();//För att börja nya rad.  
            for (int x = 0; x <= i; x++) {
                System.out.print("\t");//Moving to the place where we want to print
            }
        }
        System.out.println();
        System.out.println();

        for (int i = 0; i < row; i++) {
            int tab = 0;
            for (int j = 0; j < column; j++) {
                //setting the tab with each pass of the external loop
                //i.e, when we move to the next row. The while loop does the trick
                while (tab != (column - (i + 1))) {
                    System.out.print("\t");
                    tab++;
                }
                if (j == (column - (i + 1))) {
                    System.out.print(my2dArray1[i][j]);
                    System.out.println();//För att börja nya rad.  
                }
            }
        }
        System.out.println();
        System.out.println();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {

                if ((j == i) || (j == (column - (i + 1)))) {
                    System.out.print(my2dArray1[i][j]);
                    for (int tabs = 0; tabs <= (column - (i + 1)); tabs++) {
                        System.out.print("\t");
                    }
                }
            }
            System.out.println();//För att börja nya rad.  
        }
    }//method main ends here.
}//class TwoDArrayExp1 ends here.

Here is the output with the problematic area highlighted in red color

最佳答案

您考虑了太多条件来实现一个简单的任务,这使您的代码看起来太困惑,为什么不以老式方式打印它:

for (int i = 0; i < row; i++) {
     for (int j = 0; j < column; j++) {
          if ((i == j) || (i+j+1) == row )
              System.out.print(my2dArray1[i][j]);//print content if diagonal
          else 
              System.out.print('\t');//else print only tab space
     }
     System.out.println();
}

或者简单地说:

for (int i = 0; i < row; i++) {
     for (int j = 0; j < column; j++)
          System.out.print(((i == j) || (i+j+1) == row ) ? my2dArray1[i][j] : '\t');
     System.out.println();
}

逻辑:

  • 左对角线:i=j

  • 右对角线:(行索引 + 列索引)总和 = 总行数

输出屏幕

505 225 418 283 813 965 
802 747 969 309 871 265 
680 609 690 930 504 747 
117 862 860 700 901 720 
695 394 180 524 293 528 
934 154 303 401 795 923 

------------------------

505                 965 
    747         871     
        690 930         
        860 700         
    394         293     
934                 923 

关于java - 打印矩阵的对角线并将其显示在屏幕上,以便每个值准确显示其应在的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824411/

相关文章:

java - Spring Boot集成测试: Mock Environment Interface

java - 添加谷歌地图时gradle出错

javascript - 在javascript中迭代对象,保留 parent

matrix - 最大值中的任意大小矩阵

linux - 在 Octave 音程中,printf 给出奇怪的错误

java - 如何在不阻塞 GC 的情况下将值关联到键

java - Spring MVC 资源不映射

swift - 获取xcasset中的图像数量

css - less css 从循环中调用动态变量

c - 如何用C语言创建特定整数的 block