我正在使用 java,我想根据用户输入多次水平打印二维数组。但是,我的阵列是垂直打印的,有人可以帮忙吗?
n=3; //user input
char[][] board = new char[2][3];
char[][] f = new char[board.length][n * board[0].length];
for (int i = 1; i < n + 1; i++) {
int Start = (i * board[0].length) - board[0].length;
int End = i * board[0].length;
for (int row = 0; row < f.length; row++) {
for (int col = nStart; col < nEnd; col++) {
f[row][col] = board[row][col - nStart];
System.out.print(f[row][col]);
}
System.out.println();
}
}
For example board array =
xx
xx
I would like
xxxxxx
xxxxxx
最佳答案
如果你想水平打印二维数组,你必须在下一行之前重复打印行 n
次:
int n = 3; // user input
char[][] board = new char[][] { { 'x', 'x', 'x' }, { '0', '0', '0' } }; //example board
for (int row = 0; row < board.length; row++)
{
for (int i = 0; i < n; i++)
{
for (int col = 0; col < board[row].length; col++)
{
System.out.print(board[row][col]);
}
System.out.print("\t"); //arrays separated by tab
}
System.out.println();
}
输出:
xxx xxx xxx
000 000 000
希望对您有所帮助。
关于java - 如何多次水平打印二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47280147/