比如我现在有一个数组.
然后我循环它.每当i到达一个%10的值为0的时候就多输出\nfor(int i=0,n=array.length; i<n ;i++){
if(i%10 == 0){
System.out.print(array[i]+"\n");
}else{
System.out.print(array[i]+" ");
}
}但是为什么输处的结果是这样.为什么在第一个数就空了一行,而最后一行则少输出一个...没搞懂..0
0 0 1 1 1 2 4 4 4 6
6 7 7 7 7 8 8 8 9
然后我循环它.每当i到达一个%10的值为0的时候就多输出\nfor(int i=0,n=array.length; i<n ;i++){
if(i%10 == 0){
System.out.print(array[i]+"\n");
}else{
System.out.print(array[i]+" ");
}
}但是为什么输处的结果是这样.为什么在第一个数就空了一行,而最后一行则少输出一个...没搞懂..0
0 0 1 1 1 2 4 4 4 6
6 7 7 7 7 8 8 8 9
if(i!= 0 && i%10 == 0){
System.out.print(array[i]+"\n");
}else{
System.out.print(array[i]+" ");
}
}
// 0与10取模也为0
例如你的数组是:String s[]=new String[]{1,2,3,4,5,6,7,8,9,10};
则:s[0]=1,s[1]=2,s[2]=3.........
然后你自己想一下为什么第一行就换行了。
if(i>0 && i%10 == 0){
if((i+1)%5==0)
System.out.print(" "+i+"\n");
else
System.out.print(" "+i);
}
你数组的第一个元素为0,(0%10==0)当然要换行
for(int i=1,n=array.length; i<n ;i++) (改成这样试试)
if((i+1)%5==0)
System.out.print(array[i]+"\n");
else
System.out.print(array[i]);
}
这个就是你想要的了