【问题标题】:Printing the diagonals of the matrix and showing it on the screen so that every value shows exactly where it should be打印矩阵的对角线并将其显示在屏幕上,以便每个值准确显示它应该在的位置
【发布时间】:2014-09-13 14:59:45
【问题描述】:

我已经编写了一些代码来处理两个级别的嵌入式循环。我在主要方法结束之前的最后一段代码有问题,我只想在对角线上打印元素。代码打印值,但不是我想要查看它们的方式。当我们只写矩阵的 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.

【问题讨论】:

    标签: java loops matrix


    【解决方案1】:

    你考虑太多条件来完成一个简单的任务,这让你的代码看起来太乱了,为什么不以老派的方式打印它:

    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

    • 右对角线:sum of (row index + column index) = total row count

    输出画面

    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 
    

    【讨论】:

    • 这不是我问题的答案。你的代码做的事情和我的代码完全一样(也许是更好的方式,我同意)。感谢您的尝试,但问题出在最后一对嵌入式 for 循环中,因为它没有做我希望它为我做的事情,即以 X 的形状打印两个对角线值。
    • @AjmalKhan :好的......所以你想要两个对角线都打印???我同意上面的代码只适用于 1!!
    • 是的,最后一对嵌入式循环已经成功了,即访问表中的确切单元格以获取所需的值,但打印对我来说更棘手,因为它们看起来不像 X屏幕。
    • 请查看我刚刚添加的显示输出的图像。带红叉的图片的最后一部分显示了屏幕输出的形成有问题的地方。
    • @AjmalKhan :对于这种情况,只需修改 IF 条件...查看更新的答案!
    【解决方案2】:

    这是打印矩阵不同部分的最终代码,如下图所示:

    这是上面给出的输出的代码:

    package com.codopedia.java7.sep2014;
    

    /** * * @作者 www.codotia.com */ 公共类 TwoDArrayExp1 {

    public static void main(String args[]) {
        int row = 15, column = 15, k = 10;
        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("\nThe elements of the matrix are:");
        System.out.println();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print((my2dArray1[i][j] + "\t"));
            }
            System.out.println();
        }
        System.out.println("\nTranspose of the matrix is:");
        System.out.println();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print((my2dArray1[j][i] + "\t"));
            }
            System.out.println();
        }
    
        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++) {
                System.out.print((i == j) ? 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++) {
            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++) {
                //Left diagonal : i=j
                //right diagonal : sum of (row index + column index) = total number of rows -1 (We subtract 1 becuase array starts from zero)
                System.out.print(((i == j) || (i + j) == (row - 1)) ? my2dArray1[i][j] : " ");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if ((i + j) <= (row - 1)) {
                    System.out.print(my2dArray1[i][j] + "\t");
                }
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
    
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if ((i + j) >= (row - 1)) {
                    System.out.print(my2dArray1[i][j] + "\t");
                } else {
                    System.out.print("\t");
                }
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j <= i; j++) {
                if (((i + j) > row - 1)) {
                    continue;
                }
                System.out.print(my2dArray1[i][j] + "\t");
                if (i == j) {
                    continue;
                }
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (((i + j) >= row - 1)) {
                    System.out.print((i <= j) ? (my2dArray1[i][j] + "\t") : "\t");
                } else {
                    System.out.print("\t");
                }
            }
            System.out.println();
        }
        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(((i + j) > (row - 1)) ? "\t" : (my2dArray1[i][j] + "\t"));
                } else {
                    System.out.print("\t");
                }
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if ((i + j) >= (row - 1)) {
                    System.out.print((i >= j) ? (my2dArray1[i][j] + "\t") : "\t");
                } else {
                    System.out.print("\t");
                }
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (i == (row / 2)) {
                    System.out.print(my2dArray1[i][j] + "\t");
                } else {
                    System.out.print((j == (column / 2)) ? (my2dArray1[i][j] + "\t") : "\t");
                }
            }
            System.out.println();
            System.out.println();
        }
        System.out.println();
        System.out.println();
    }//method main ends here.
    

    }//类 TwoDArrayExp1 到此结束。

    【讨论】:

    • 尝试为每个对角线定义方法,你的代码一团糟!
    猜你喜欢
    • 1970-01-01
    • 2022-06-11
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多