建立了两组数列
for (int row = 0; row < board_size; row++) {
for (int col = 0; col < board_size; col++) {
System.out.print(theBoard[row][col]);
}
System.out.println();
}
我现在需要把这两组数列以如下形式展现出来,是两个只有3个数字的数列,应该怎么样做? | |
-----
| |
_____
| |
-----
for (int row = 0; row < board_size; row++) {
for (int col = 0; col < board_size; col++) {
System.out.print(theBoard[row][col]);
}
System.out.println();
}
我现在需要把这两组数列以如下形式展现出来,是两个只有3个数字的数列,应该怎么样做? | |
-----
| |
_____
| |
-----
import java.util.Arrays;
public class TwoArrays
{
public static void main(String args[])
{
int arr[][] = new int[][]{{1,2,3},{4,5,6}};
for(int i = 0;i < arr.length;i++)
{
for(int j = 0;j < arr[i].length;j++)
System.out.print(arr[i][j]);
System.out.println();
}
}
}
import java.util.Arrays;
public class TwoArrays
{
public static void main(String args[])
{
int arr[][] = new int[][]{{1,2},{3,4},{5,6}};
for(int i = 0;i < arr.length;i++)
{
for(int j = 0;j < arr[i].length;j++)
System.out.print(arr[i][j]);
System.out.println();
}
}
}
建立了一个2d的array
for (int row = 0; row < board_size; row++) {
for (int col = 0; col < board_size; col++) {
System.out.print(theBoard[row][col]);
}
System.out.println();
}
通过上述代码我可以把这个2d array都一个个显示出来,但是我这个是是两个3个元素的array(看的英文版教材。。)画出图来就是3X3的正方形,我想通过System.out.println这个方法把这几个元素显示出来,格式如下(x代表array里的数字)
x|x|x
-----
x|x|x
-----
x|x|x
这样解释会不会好一些?
什么叫“是是两个3个元素的array”
“我想通过System.out.println这个方法把这几个元素显示出来”--- 你的意思是打印下面这样的数字?x|x|x
-----
x|x|x
-----
x|x|x总之,我晕了
比如
横着0,1,2
竖着0,1,2
那最后一共就是9个数字,我想通过那样的方法把9个数字一次打印出来,这样说会不会好一点
想打成一个3*3的正方形?
想打成3*3的正方形。。
太对了!!!!!!!!!!!!!!!!!!!
用'|'和----把这6个隔离开来,为了更方便阅读!!!!!!!!!!!!!!!!
终于有明白人了激动的我真是内牛满面
高人你有办法吗????????就用system.out.println做出这种格式,像我前面贴的似的,有两个for分别对应那个正方形的两个坐标。。希望这次不要出现理解上的差错了
for (int row = 0; row < board_size; row++) {
for (int col = 0; col < board_size; col++) {
System.out.print(theBoard[row][col]);
}
System.out.println();
}
我现在改成这样了,显示结果很怪异,麻烦高人了。。
for (int row = 0; row < board_size; row++) {
for (int col = 0; col < board_size; col++) {
System.out.print(theBoard[row][col]);
if (col==0){System.out.print('|');}
if (col==1){System.out.print('|'+"\n");}
if (col==2&&row!=2){System.out.print("-----"+"\n");}
}
}
public static void main(String[] args) {
int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Test.printArray(a);
} public static void printArray(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (j != a[i].length - 1) {
System.out.print(a[i][j] + " | ");
} else {
System.out.print(a[i][j]);
}
}
System.out.println();
System.out.println("----------");
}
}
}
public static void main(String[] args) {
int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Test.printArray(a);
} public static void printArray(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (j != a[i].length - 1) {
System.out.print(a[i][j] + " | ");
} else {
System.out.print(a[i][j]);
}
}
System.out.println();
if (i != a.length - 1) {
System.out.println("----------");
}
}
}
}
这个不会打印最后一行---------------