0

我是java中数组的新手。 如果我想显示此:在字符数组之前/之前添加一些值

0 .... 
    1 .... 
    2 ....  
    3 .... 
    4 .... 

其中:

.... 
    .... 
    .... 
    .... 
    ....  is a 2-d char array called char[][] squares. 

如何正方形排列前添加数字?

而且,如果我选择一个数字,比如说3,并且想在3旁边加上“>”,我应该怎么做?所以,我要的是:

0 .... 
    1 .... 
    2 ....  
    3>.... 
    4 .... 

回答

0
for(int y = 0; y< array.lenght;y++) { 
    for(int x = 0; x< array[y].lenght;x++) { 
     System.out.println(array[y][x]); 
    } 

    System.out.println(); 
} 
0

试试这个

private void print_array(char[][] squares, int selectedIndex){ 
    for(int i =0;i<squares.length;i++){ 
     System.out.print(i); 
     if(i == selectedIndex){ 
      System.out.print(">"); 
     } 
     for(int j = 0;j<squares[i].length;j++){ 
      System.out.print(squares[i][j]); 
     } 
     System.out.println(); 
    } 
} 
0

我不知道,如果这种问题是允许的,但在这里就是答案。我们假设你有字符[m] [n]数组。 和你想提前X线打印>

for(int i = 0; i < m; i++) 
{ 
     String s = i+""; 
     if(i == x) s = s + ">"; 
     else  s = s+ " "; 
     s = s + new String(chars[i]) 
     System.out.println(s); 
} 
0

你提的问题是非常模糊的,但我给它一个镜头:

class Whatever { 

public static void Main(String[] args) { 

char[][] squares = new char[5][2]; 

/* 
Here goes your code to assign values to the array 
*/ 

for(int i:=0, i<5; ++i) { 
    System.out.println(i); 
    if (i==3) { 
     System.out.print("> "); 
    } else { 
     System.out.print(" ") 
    } 
    for(j:=0; j<2; ++j) { 
     System.out.println(aquares[i][j]); 
    } 
} 
} 
相关问题