[英]How to make a new Line every 8th input
My code requires me to create an array (from user input), display it backwards, and find the sum of each number.我的代码要求我创建一个数组(从用户输入),向后显示它,并找到每个数字的总和。 So far I have been able to accomplish all requirements.
到目前为止,我已经能够完成所有要求。 However, if the array is more than 8 numbers, then when it is displayed the program must create a new line every 8th number.
但是,如果数组超过 8 个数字,那么当它显示时,程序必须每隔 8 个数字创建一个新行。 I'm having difficulty accomplishing this goal.
我很难实现这个目标。 Here is my code so far:
到目前为止,这是我的代码:
import java.util.Scanner;
public class arrayCreator {
public static void main(String[] args) {
int length;
double sumArray = 0;
Scanner input = new Scanner(System.in);
System.out.print("How many elements in the array? ");
length = input.nextInt();
}
for(int j = currentArray.length-1; j >= 0; j-- )
{
System.out.printf("%.3f \t", currentArray[j]);
if(currentArray.length - 8 == j) // here is where I'm having the problem
{
System.out.print("\n");
}
input.close();
}
}
What should go inside of the if statement in order to create a new line each time 8 inputs are displayed?为了在每次显示 8 个输入时创建一个新行,if 语句中的 go 应该是什么?
This is what output should look like:这就是 output 的样子:
How many elements in the array?
数组中有多少个元素? 20
20
Please enter the next value 1
请输入下一个值 1
Please enter the next value 2
请输入下一个值 2
Please enter the next value 3
请输入下一个值 3
Please enter the next value 4
请输入下一个值 4
Please enter the next value 5
请输入下一个值 5
Please enter the next value 6
请输入下一个值 6
Please enter the next value 7
请输入下一个值 7
Please enter the next value 8
请输入下一个值 8
Please enter the next value 9
请输入下一个值 9
Please enter the next value 10
请输入下一个值 10
Please enter the next value 11
请输入下一个值 11
Please enter the next value 12
请输入下一个值 12
Please enter the next value 13
请输入下一个值 13
Please enter the next value 14
请输入下一个值 14
Please enter the next value 15
请输入下一个值 15
Please enter the next value 16
请输入下一个值 16
Please enter the next value 17
请输入下一个值 17
Please enter the next value 18
请输入下一个值 18
Please enter the next value 19
请输入下一个值 19
Please enter the next value 20
请输入下一个值 20
20.000 19.000 18.000 17.000 16.000 15.000 14.000 13.000
20.000 19.000 18.000 17.000 16.000 15.000 14.000 13.000
12.000 11.000 10.000 9.000 8.000 7.000 6.000 5.00012.000 11.000 10.000 9.000 8.000 7.000 6.000 5.000
4.000 3.000 2.000 1.0004.000 3.000 2.000 1.000
The Sum of the array's elements is: 210.000
数组元素的总和是:210.000
The other answer isn't working correctly because you're backing up through the list from the end back to the beginning, but the mod operator causes line breaks as if you were moving from the beginning to the end.另一个答案无法正常工作,因为您正在从列表的末尾备份到开头,但是 mod 运算符会导致换行符,就好像您从开头移动到结尾一样。 However, the idea of using the modulo operator is definitely correct.
但是,使用模运算符的想法绝对是正确的。 Do this in your if statement:
在您的 if 语句中执行此操作:
if((length - j) % 8 == 0) {
System.out.print("\n");
}
Usually when you want to do something every n times, you want to use modulo division : %
.通常当你想每n次做某事时,你想使用模除法:
%
。
Change this改变这个
if(currentArray.length - 8 == j) // here is where I'm having the problem
{
System.out.print("\n");
}
To this对此
if (j % 8 == 0) // here is where I'm having the problem
{
System.out.print("\n");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.