Answer to Question #281599 in Java | JSP | JSF for Andrew

Question #281599

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.


1
Expert's answer
2021-12-20T15:29:15-0500
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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog