简体   繁体   English

java 中的数组元素之和

[英]Sum of array elements in java

I want to do the sum of array elements.我想做数组元素的总和。 I made this but I can't see what is my mistake?我做了这个,但我看不出我的错误是什么? Thanks in advance.提前致谢。

Input Format输入格式

The first line contains a single integer N denoting the size of the array.第一行包含一个 integer N 表示数组的大小。 (eg 5) The next line contains space-separated integers denoting the elements of the array. (例如 5)下一行包含用空格分隔的整数,表示数组的元素。 (eg 1000000001 1000000002 1000000003 1000000004 1000000005) (例如 1000000001 1000000002 1000000003 1000000004 1000000005)

Tried following Different ways尝试以下不同的方式

import java.io.BufferedReader;
import java.io.InputStreamReader;

//import for Scanner and other utility classes
import java.util.*;


public class TestClass {
    public static void main(String args[] ) throws Exception {
        int sumofArray = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Size of Array :- ");
        int totalNumbers = Integer.parseInt(br.readLine());
        System.out.println("totalNumbers=> " + totalNumbers);
        int []arrayEle = new int[totalNumbers];
        System.out.println("Enter Array element :- ");
        String arrEle = br.readLine();
        System.out.println("Array Elements Are:-"+arrEle);
        String []myArray = arrEle.split(" ",totalNumbers+1);
        for(int i = 0; i < totalNumbers; i++) {
            arrayEle[i] = Integer.parseInt(myArray[i]);
            System.out.println(arrayEle[i]);
            //sumofArray = sumofArray + arrayEle[i];
            //System.out.println("i= "+i+" arrayEle[i]= "+arrayEle[i] + " sumofArray= "+sumofArray);
        }
        for(int i = 0; i < totalNumbers; i++) 
            sumofArray += arrayEle[i];
        System.out.println("Sum of Array Elements is:-  "+sumofArray);
        //Scanner s = new Scanner(System.in);
        //String name = s.nextLine();

    }
}

But unable to understand, why i'm ending with this result但无法理解,为什么我会以这个结果结束

Enter Size of Array :- 
totalNumbers=> 5
Enter Array element :- 
Array Elements Are:-1000000001 1000000002 1000000003 1000000004 1000000005
1000000001
1000000002
1000000003
1000000004
1000000005
Sum of Array Elements is:-  705032719

Your sumofArray variable is int.您的 sumofArray 变量是 int。 When you add those 5 numbers, integer variable can't store the value.当您添加这 5 个数字时,integer 变量无法存储该值。 The upper limit of int value in java is 2147483647. Therefore, you are getting an unexpected answer. java中int值的上限是2147483647。因此,你得到了一个意想不到的答案。 If you change the data type of sumofArray to long, it will work.如果将 sumofArray 的数据类型更改为 long,它将起作用。

In java, int, long and double is primitive data types that have some max length in memory.在 java 中,int、long 和 double 是原始数据类型,在 memory 中具有一定的最大长度。

int: -2147483648 to 2147483647整数: -2147483648 to 2147483647

you can think of it as a loop, when your limit increases it doesn't throw an error.您可以将其视为一个循环,当您的限制增加时,它不会引发错误。 If you will add 2147483647+1 to the max number it will give you -2147483648如果您将 2147483647+1 添加到最大数量,它将给您 -2147483648

int a = 2147483647;
a+1; // -21474836486

It's true for every primitive data type so you have to always look for the max values of every data types.对于每种原始数据类型都是如此,因此您必须始终寻找每种数据类型的最大值。

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

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