deyanmalinov

06. Find Evens or Odds

Apr 12th, 2020
214
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. import java.util.function.Predicate;
  3. import java.util.stream.IntStream;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         String[] line = scan.nextLine().split(" ");
  9.         String evenOrOdd = scan.nextLine();
  10.         Predicate<Integer> even = x -> x % 2 == 0;
  11.         Predicate<Integer> odd = x -> x % 2 != 0;
  12.         int start = Integer.parseInt(line[0]);
  13.         int end = Integer.parseInt(line[1]);
  14.         Predicate<Integer> type = even;
  15.         if (evenOrOdd.equals("odd")){
  16.             type = odd;
  17.         }
  18.         IntStream.rangeClosed(start, end)
  19.                 .boxed()
  20.                 .filter(type)
  21.                 .forEach(x -> System.out.printf("%d ", x));
  22.     }
  23. }
RAW Paste Data