题目:自定义一个数组,(这是我自己定义的int[] arr = {12,12,23,34,45,54}),要求每三位进行一次换行打印,打印格式[a,b,c][d,e,f]
/*
[12,12,23]
[34,45,54]
*/
class Demo {
public static void main(String[] args) {
int[] arr = {12,12,23,34,45,54};
print(arr);
}
public static void print(int[] arr) {
for (int x = 0;x < arr.length ;x++ ) {
if (x % 3 == 0) {
System.out.print("[" );
}
System.out.print( arr[x] +"," );
if ((x+1) % 3 == 0) {
System.out.println("]");
}
}
}
}
但是打印出来的结果是
[12,12,23,]
[34,45,54,]
有哪位大神知道这个怎么改吗? |
|