开发者

How do i output 1-d arrays in a set #(10) per line?

i know i can use many if statements together but i think thats annoying, is there any better way?

    for(index=0; index<alpha.len开发者_如何学Cgth; index++)
    {
        System.out.print(alpha[index]+ "");
        if (index == 9)
            System.out.println();
        if (index == 19)
            System.out.println();
        if (index == 29)
            System.out.println();
        if (index == 39)
            System.out.println();
    }


if ((index + 1) % 10 == 0) 

This(%) is a rest of division.


Use an inner loop (that iterates from 0 to 9 inclusive).


Use the % operator like this:

for(index=0; index<alpha.length; index++)
{
    System.out.print(alpha[index]+ "");
    if ( ( ( index + 1 ) % 10 ) == 0 ) {
        System.out.println();
    }
}


for(index=0; index<alpha.length; index++)
{
    System.out.print(alpha[index]+ "");
    if (index % 10 == 9)
        System.out.println();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜