Write a program to create an array in order to store 15 numbers then display only even numbers in one line and also display how many even numbers found.
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
private static final int NUMBERS_SIZE = 15;
public static void main(String[] args) {
int numbers[] = new int[NUMBERS_SIZE];
Scanner input = new Scanner(System.in);
for (int i = 0; i < NUMBERS_SIZE; i++) {
while (true) {
try {
numbers[i] = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("Wrong data, try again...");
input.next();
continue;
}
break;
}
}
int evenCounter = 0;
for (int i = 0; i < NUMBERS_SIZE; i++) {
if (numbers[i] % 2 == 0) {
evenCounter++;
System.out.print(numbers[i] + " ");
}
}
System.out.println();
System.out.println("Total even numbers: " + evenCounter);
}
}
Comments
Leave a comment