Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lekciq;
- import java.util.Scanner;
- import java.util.function.IntPredicate;
- import java.util.stream.IntStream;
- public class FindEvensOrOdds {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read lower and upper bounds
- int lowerBound = scanner.nextInt();
- int upperBound = scanner.nextInt();
- scanner.nextLine(); // Consume newline
- // Read command
- String command = scanner.nextLine();
- // Define predicate based on command
- IntPredicate predicate;
- if (command.equals("even")) {
- predicate = number -> number % 2 == 0;
- } else if (command.equals("odd")) {
- predicate = number -> number % 2 != 0;
- } else {
- throw new IllegalArgumentException("Invalid command: " + command);
- }
- // List numbers in the range based on the predicate
- listNumbers(lowerBound, upperBound, predicate);
- }
- // Method to list numbers in the range based on the predicate
- private static void listNumbers(int lowerBound, int upperBound, IntPredicate predicate) {
- IntStream.rangeClosed(lowerBound, upperBound)
- .filter(predicate)
- .forEach(num -> System.out.print(num + " "));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement