Java 如何在数组中打印特定的数字?

Java 如何在数组中打印特定的数字?,java,arrays,Java,Arrays,我试图在对角线上打印相应的数字,但我只能直接向下打印数字。它打印出2,1,7,0,5。但是输出不是按对角线打印的。有人能帮我吗 public class Main_diagonal { public static void main(String[] args) { int array1[][] = { {2,3,1,5,0 }, {7,1,5,3,1 }, {2,5,7,8

我试图在对角线上打印相应的数字,但我只能直接向下打印数字。它打印出2,1,7,0,5。但是输出不是按对角线打印的。有人能帮我吗

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

        int array1[][] = {
                {2,3,1,5,0 },
                {7,1,5,3,1 },
                {2,5,7,8,1 },
                {0,1,5,0,1 },
                {3,4,9,1,5 }
        };



        for (int i=0; i<5; i++)
        {

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

                    System.out.println(array1[i][j]);
                }

            }
        }

    }
}
公共类主\u{
公共静态void main(字符串[]args){
int array1[]]={
{2,3,1,5,0 },
{7,1,5,3,1 },
{2,5,7,8,1 },
{0,1,5,0,1 },
{3,4,9,1,5 }
};

对于(int i=0;i我不确定您使用的是哪种语言,因此在当前状态下,这可能不适用于您,但您知道了

一旦你透露了你使用的语言,我们可以优化:)

字符串空白=”;
对于(int i=0;i如果“在对角线上打印数字”,则意味着您需要使输出以对角线显示,如下所示:

2
 1
  7
   0
    5
    for (int i=0; i<5; i++)
    {
        for (int j=0; j<=i;j++)
        {   
            if(i==j){
                System.out.println(array1[i][j]);
                break;
            }
            System.out.print(" ");              
        }
    }
然后,让你的内部循环打印空间,它不会打印数字

    for (int i=0; i<5; i++)
    {
        for (int j=0; j<=i;j++)
        {   
            if(i==j) { //then print the number and a new line
                System.out.println(array1[i][j]);
            }
            else {
                System.out.print(" "); //pads the line with spaces otherwise
            }
        }
    }

for(int i=0;i修改代码如下:

2
 1
  7
   0
    5
    for (int i=0; i<5; i++)
    {
        for (int j=0; j<=i;j++)
        {   
            if(i==j){
                System.out.println(array1[i][j]);
                break;
            }
            System.out.print(" ");              
        }
    }

for(int i=0;i因此请提供实际输出和预期输出。您似乎已经在打印实际对角线了。