Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打印矩阵的对角线并将其显示在屏幕上,以便每个值都能准确地显示其位置_Java_Loops_Matrix - Fatal编程技术网

Java 打印矩阵的对角线并将其显示在屏幕上,以便每个值都能准确地显示其位置

Java 打印矩阵的对角线并将其显示在屏幕上,以便每个值都能准确地显示其位置,java,loops,matrix,Java,Loops,Matrix,我已经编写了一些代码在两个级别上使用嵌入式循环。在主要方法结束之前,我对代码的最后一部分有问题,我只想在对角线处打印元素。代码打印值,但不是我希望看到它们的方式。我被给定的标签数量所困扰,所以当我们只写矩阵的数字时,值会以它在纸上显示的方式打印出来 这是我的密码: package com.codopedia.java7.sep2014; /** * * @author www.codopedia.com */ public class TwoDArrayExp1 { publi

我已经编写了一些代码在两个级别上使用嵌入式循环。在主要方法结束之前,我对代码的最后一部分有问题,我只想在对角线处打印元素。代码打印值,但不是我希望看到它们的方式。我被给定的标签数量所困扰,所以当我们只写矩阵的数字时,值会以它在纸上显示的方式打印出来

这是我的密码:

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.
package com.codopedia.java7.sep2014;
/**
*
*@author www.codopedia.com
*/
公共类TwoDArrayExp1{
公共静态void main(字符串参数[]){
int行=5,列=5,k=0;
int my2dArray1[][]=新的int[row][column];//5行5列
//将数组元素初始化为零
对于(int i=0;ii){//当列大于行时(仅打印在双对角线上及其下方。)
继续;//在此行停止打印并转到下一行
}
//系统输出打印(“(”+(i+1)+“,“+(j+1)+”)“+”=“+”+my2dArray1[i][j]+“\t”);
系统输出打印(my2dArray1[i][j]+“\t”);
}
System.out.println();//För att börja nya rad。
}
System.out.println();
System.out.println();
对于(int i=0;i=i){//仅在对角线上及其上方开始。
系统输出打印(my2dArray1[i][j]+“\t”);
}
}
System.out.println();//För att börja nya rad。

对于(int x=0;x您考虑的实现简单任务的条件太多,使您的代码看起来太混乱,为什么不以老式方式打印它:

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();
}

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

以下是上述输出的代码:

package com.codopedia.java7.sep2014;
/** * *@author www.codopedia.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.
publicstaticvoidmain(字符串参数[]){
int行=15,列=15,k=10;
int my2dArray1[][]=新的int[row][column];//5行5列
//将数组元素初始化为零
对于(int i=0;ii){//当列大于行时(仅打印在双对角线上及其下方。)
继续;//在此行停止打印并转到下一行
}
//系统输出打印(“(”+(i+1)+“,“+(j+1)+”)“+”=“+”+my2dArray1[i][j]+“\t”);
系统输出打印(my2dArray1[i][j]+“\t”);
}
System.out.println();//För att börja nya rad。
}
System.out.println();
System.out.println();
对于(int i=0;i=i){//仅在对角线上及其上方开始。
系统输出打印(my2dArray1[i][j]+“\t”);
}
}
System.out.println();//För att börja nya rad。
对于(int x=0;x=row-1)){
系统输出打印((i=(第1行)){
系统输出打印((i>=j)?(my2dArray1[i][j]+“\t”):“\t”);
}否则{
系统输出打印(“\t”);
}
}
System.out.println();
}
System.out.println();
System.out.println();
对于(int i=0;i

}//类TwoDArrayExp1到此结束。

这不是我问题的答案。您的代码做的事情完全相同(我同意,也许是更好的方式)正如我的代码所做的。感谢您的尝试,但问题出在最后一对嵌入式for循环中,因为它没有完成我希望它为我做的事情,即,在形状o中打印两个对角线值
package com.codopedia.java7.sep2014;
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.