for (int i = Array1.length - 1; i >= 0; i-- ) {
System.out.print(Array1[i] + " ");
}
嗨,如何在控制台中显示的第8个元素之后添加一行?
6.7 3.4 6.7 1.2 ...
(I need the rest of the elements after the 8th to be displayed on the next line here)
The sum of the array is: 18.0
答案 0 :(得分:1)
您可以对当前索引进行简单检查,然后在索引匹配时打印新行。
for (int i = Array1.length - 1; i >= 0; i-- ) {
if ((i != Array1.length-1) && ((Array1.length - i - 1)%8 == 0)) {
System.out.println();
}
System.out.print(Array1[i] + " ");
}
答案 1 :(得分:0)
您可以使用长度和索引来打印它:
for (int i = Array1.length - 1; i >= 0; i-- ) {
if (Array1.length - i == 8) {
System.out.println();
}
System.out.print(Array1[i] + " ");
}
答案 2 :(得分:0)
使用流,您可以使用以下内容:-
IntStream.range(0, Array1.length)
.mapToObj( i -> Array1[i] + (i > 0 && i % 7 == 0 ? "\n": " "))
.forEach(System.out::print);