Advertisement
Spocoman

03. Sum Even Numbers

Oct 20th, 2024 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int sum = Arrays                                    // "1 2 3 4 5 6 7 8 9"
  9.                 .stream(scanner.nextLine().split(" "))     // ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
  10.                 .mapToInt(Integer::parseInt)              // [1, 2, 3, 4, 5, 6, 7, 8, 9]
  11.                 .filter(x -> x % 2 == 0)                 // [2, 4, 6, 8]
  12.                 .sum();                                 // 2 + 4 + 6 + 8
  13.  
  14.         System.out.println(sum);                      // 20
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement