描述
给定一个数组 A,数组 A 共含有 n 个整数,计算数组 A 内所有整数的和。请通过 System.out.println 语句输出数组 A 的和。
0 ⩽ n ⩽ 100000 0 \leqslant n \leqslant100000 0⩽n⩽100000
− 100 ⩽ A [ i ] ⩽ 100 − 100 ⩽ A [ i ] ⩽ 100 -100 \leqslant A[i] \leqslant 100−100⩽A[i]⩽100 −100⩽A[i]⩽100−100⩽A[i]⩽100
样例
评测机会将整个项目的代码编译为一个可执行的 Main 程序,并按照这样的方式执行你的代码 Main,你的代码需要从标准输入流(控制台)中读入数据 n 和 A(共 22 行,第 11 行包含 11 个整数 n,第 22 行包含 n 个整数,以空格分隔),计算出结果后并打印到标准输出流(控制台)中。
样例 1:
当 n = 2,A[0] = 1,A[1] = 2 时,应该打印
3
样例 2:
当 n = 0 时,应该打印
0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
// read data from console
// output the answer to the console according to the
// requirements of the question
Scanner sc = new Scanner(System.in); // 创建一个新的类scanner用于获取输入
int n = sc.nextInt();
int sum = 0;
for (int i = 0; i < n; i++){
sum += sc.nextInt(); //for循环遍历数组
}
System.out.println(sum);
}
}