-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindEvensOrOdds.java
30 lines (28 loc) · 1002 Bytes
/
FindEvensOrOdds.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package Lab;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Predicate;
public class FindEvensOrOdds {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String [] range = scan.nextLine().split(" ");
int beginning = Integer.parseInt(range[0]);
int ending = Integer.parseInt(range[1]);
String command = scan.nextLine();
Predicate<Integer> filterEvenOrOdd;
if(command.equals("even")){
filterEvenOrOdd = number -> number % 2 == 0;
}else{
filterEvenOrOdd = number -> number % 2 != 0;
}
List<Integer> numbers = new ArrayList<>();
for (int i = beginning; i <= ending ; i++) {
boolean isTrue = filterEvenOrOdd.test(i);
if(isTrue) {
numbers.add(i);
}
}
numbers.forEach(e -> System.out.print(e + " "));
}
}