desislava_topuzakova

01. Train

Jun 5th, 2021
1,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class Demo_2 {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int n = Integer.parseInt(scanner.nextLine()); //брой на вагоните
  8.         int [] peopleInWagons = new int[n]; //[12, 24, 8]
  9.  
  10.         for (int index = 0; index <= peopleInWagons.length - 1; index++) {
  11.             peopleInWagons[index] = Integer.parseInt(scanner.nextLine());
  12.         }
  13.        
  14.         //всеки елемент от масива -> принтирам -> сумирам
  15.         int sumPeople = 0;
  16.         for (int countPeople : peopleInWagons) {
  17.             System.out.print(countPeople + " ");
  18.             sumPeople += countPeople;
  19.         }
  20.  
  21.         System.out.println();
  22.         System.out.println(sumPeople);
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment