gpt4 book ai didi

java - 简化多线阵列

转载 作者:行者123 更新时间:2023-12-01 13:50:03 25 4
gpt4 key购买 nike

我对 Java 还是个新手,想知道是否有一种方法可以减少我的数组,以便当 5 进入一个数字并且没有剩下的东西时,利用余数运算符开始一个新行?从今天早上开始我一直在试图解决这个问题,但似乎没有取得任何进展。提前谢谢你的帮助。

    public class arrayTest {

public static void main(String args[]) {
final int ARRAY_LENGTH = 25; // number of ints
int array[] = new int[ ARRAY_LENGTH ]; // calculate value for each array element

for (int counter = 0; counter < 5; counter++ )
System.out.printf("%d, ", counter);
for (int counter = 5; counter < 6; counter++ )
System.out.printf("%d\n", counter);
for (int counter = 6; counter < 10; counter++ )
System.out.printf("%d, ", counter);
for (int counter = 10; counter < 11; counter++ )
System.out.printf("%d\n", counter);
for (int counter = 11; counter < 15; counter++ )
System.out.printf("%d, ", counter);
for (int counter = 15; counter < 16; counter++ )
System.out.printf("%d\n", counter);
for (int counter = 16; counter < 20; counter++ )
System.out.printf("%d, ", counter);
for (int counter = 20; counter < 21; counter++ )
System.out.printf("%d\n", counter);
for (int counter = 21; counter < 25; counter++ )
System.out.printf("%d, ", counter);
for (int counter = 25; counter < 26; counter++ )
System.out.printf("%d\n", counter);
}

}

最佳答案

public class arrayTest
{

public static void main( String args[] )
{
final int ARRAY_LENGTH = 25; // number of ints
int array[] = new int[ ARRAY_LENGTH ];// calculate value for each array element
for (int counter = 0; counter < array.length; counter++ ) {
System.out.printf("%d, ", counter);
if (counter%5 == 4) {
System.out.printf("\n"); // System.out.println() also works
}
}
}

}

关于java - 简化多线阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20037036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com