简体   繁体   English

减去单个数组中的所有元素 Java

[英]Subtracting all elements in single array Java

Hi I'm having trouble subtracting one array element to the next element to get the correct answer.嗨,我无法将一个数组元素减去下一个元素以获得正确答案。 The value of the array are given by the user.数组的值由用户给出。

Example: If the user wants to input 3 numbers which are 10, 8, 1 10-8-1 = 1示例:如果用户要输入 3 个数字,即 10、8、1 10-8-1 = 1

int numberOT = 0;
int total =0;
System.out.println("Enter Number of times: ");
numberOT =in.nextInt();

int number[] = new int [numberOT];



for(int i = 0; i<numberOT; i++)
{
    System.out.println("Enter Number: ");
    number[i] = in.nextInt();
}
for(int t =0; t<number.length-1; t++)
{
  total = number[t] - number[t+1];
}
System.out.println("total: " + total);

Change the 2nd loop to this:将第二个循环更改为:

total = number[0];
for (int i=1; i<number.length; i++) {
    total -= number[i];
}

You want to subtract the remaining array items from the first one.您想从第一个数组项中减去剩余的数组项。 Therefore total in the beginning should be equal to the first item and in the loop subtract each consequent (start from the index 1 ) item from the total .因此,开始的total应该等于第一项,并且在循环中从total中减去每个后续项(从索引1开始)。

Remember the number of items in the array must be equal to or larger than 2.请记住,数组中的项目数必须等于或大于 2。

this line is totally wrong: total = number[t] - number[t+1];这一行是完全错误的: total = number[t] - number[t+1]; as in the last loop和最后一个循环一样

total = number[1] - number[2] which will be equivalent to total = 8 - 1 = 7 which is totally wrong because you have to accumulate the total variable. total = number[1] - number[2]这将等同于total = 8 - 1 = 7这是完全错误的,因为您必须累积总变量。

so as mentioned above the full correct answer code is:所以如上所述,完整的正确答案代码是:

    import java.util.Scanner;

    public class Main
    {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);

            int numberOT = 0;
            int total =0;
            System.out.println("Enter Number of times: ");
            numberOT =in.nextInt();

            int number[] = new int [numberOT];

            for(int i = 0; i<numberOT; i++)
            {
                System.out.println("Enter Number: ");
                number[i] = in.nextInt();
            }
            total = number[0];
            for (int i=1; i<number.length; i++) {
                total = total - number[i];
            }
            System.out.println("total: " + total);

        }
    }

and here is image of the output:这是 output 的图像:

[ [在此处输入图像描述 ][1] ][1]

Here is a one liner (computation wise) using streams.这是一个使用流的单列(计算方式)。 Take the first element, stream the array skipping the first element and subtract the sum of the remaining.取第一个元素 stream 数组跳过第一个元素并减去剩余的总和。

int[] arr = { 10, 8, 3, 1 };
int sum = arr[0] - IntStream.of(arr).skip(1).sum();
System.out.println(sum);

But no matter how you do it, you have the potential for int overflow or underflow depending on the size of your values and the length of the array.但无论您如何操作,根据值的大小和数组的长度,您都有可能发生int上溢或下溢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM