请你分别求出每个数组的元素之和。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int n = 0 ; if(in.hasNextInt()){ n = in.nextInt(); } for(int i=0;i<n;i++){ int num=0; if(in.hasNextInt()){ num = in.nextInt(); } long sum = 0; for(int j=0;j<num;j++){ if(in.hasNextInt()) { sum = sum +in.nextInt(); } } System.out.println(sum); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int i = in.nextInt(); while (0<i--) { // 注意 while 处理多个 case int a = in.nextInt(); long b = 0; while(0<a--){ b = b + in.nextInt(); } System.out.println(b); } } }
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a=in.nextInt(); long sum=0; for(int b=0;b<a;b++){ int d=in.nextInt(); for(int c=0;c<d;c++){ int x=in.nextInt(); sum=x+sum; } System.out.println(sum); sum=0; } } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 long num = in.nextLong(); for(long i = 0; i<num;i++){ long every = in.nextLong(); long eOut = 0; while(in.hasNextLong()){ eOut = eOut + in.nextLong(); every--; if(every == 0){ break; } } System.out.println(eOut); } in.close(); }