CoineTre

JF-ExcArrays01.Train

Jan 20th, 2021 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. /*You will be given a count of wagons in a train n. On the next n lines,
  2. you will receive how many people are going to get on that wagon.
  3. At the end print the whole train and after that the sum of the people in the train.*/
  4.  
  5. import java.util.Scanner;
  6. public class TestXXX {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int nWagons = scanner.nextInt();
  10.         int[] train = new int[nWagons];
  11.         int sum = 0;
  12.         for (int i = 0; i < train.length ; i++) {
  13.             train [i] = scanner.nextInt();
  14.             sum += train[i];
  15.         }
  16.         for (int waggons : train) {
  17.             System.out.printf("%d ",waggons);
  18.         }
  19.         System.out.println();
  20.         System.out.println(sum);
  21.     }
  22. }
Add Comment
Please, Sign In to add comment