-2

i want setting an array and below is my code

    public static void setArray()
{
    int i = 5;
    int j = 5;
    int testarray[][] = new  int[i][j];

    for(int x = 0;x<i;x++)
    {
        for(int y=0;y<j;y++)
        {
            System.out.print("0 ");
        }
        System.out.println("");     
    } 
}

the result is something like this: 0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

if i want to put a number/alphabet beside to let the user know which column, how can i do that ?

Expected Result:

====================

1 2 3 4 5

A|0 0 0 0 0

B|0 0 0 0 0

C|0 0 0 0 0

D|0 0 0 0 0

E|0 0 0 0 0

4

2 回答 2

2

您需要另一个初始 for 循环来打印数字,然后您需要在第二个 for 循环中添加另一个打印语句来打印每一行的字母:

System.out.print(" ");
for (int x = 0; x < i; x++) {  // this prints the numbers on the first row
    System.out.print(" " + x);
}
System.out.println();

for (int x = 0; x < i; x++) {
    System.out.print((char) ('A' + x) + "|");  // this prints the letters
    for (int y = 0; y < j; y++) {
        System.out.print("0 ");
    }
    System.out.println("");
}
  0 1 2 3 4
A|0 0 0 0 0
B|0 0 0 0 0
C|0 0 0 0 0
D|0 0 0 0 0
E|0 0 0 0 0
于 2013-01-12T15:21:25.467 回答
0

您需要打印 1、2、3、4、5 ..column次数并打印 A、B、C、D .. 直到达到rows 的数量。自己尝试编码,没那么难(我不想提供现成的代码)

于 2013-01-12T15:17:36.197 回答