Java—A+B for Input-Output Practice

小灰灰 2022-12-06 01:18 246阅读 0赞

Java—A+B for Input-Output Practice

Description
Your task is to calculate the sum of some integers
Input
Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line
Output
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.
Sample
Input
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3
Output
10

15

6

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. Scanner reader = new Scanner(System.in);
  6. int a, m, n, i, j, sum;
  7. m = reader.nextInt();
  8. for (i = 0; i < m; i++) {
  9. sum = 0;
  10. n = reader.nextInt();
  11. for (j = 0; j < n; j++) {
  12. a = reader.nextInt();
  13. sum = sum + a;
  14. }
  15. System.out.println(sum);
  16. if (i != m - 1) {
  17. System.out.print("\n");
  18. }
  19. }
  20. reader.close();
  21. }
  22. }

发表评论

表情:
评论列表 (有 0 条评论,246人围观)

还没有评论,来说两句吧...

相关阅读