Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.0 kB
5
Indexable
Never
package org.example;

import java.util.*;

public class ArrayAnalyzerB {

    private int[] arr;

    public ArrayAnalyzerB(int[] arr) {
        this.arr = arr;
    }

    public int countInRange(int minVal, int maxVal) {
        if (arr == null || arr.length == 0) {
            return 0; // Return 0 if the array is null or empty
        }

        int count = 0;
        for (int num : arr) {
            if (minVal <= num && num < maxVal) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter minimum value: ");
        int minVal = scanner.nextInt();
        System.out.print("Enter maximum value: ");
        int maxVal = scanner.nextInt();

        int[] array = {14, 1, 22, 7, -43, 5};
        ArrayAnalyzerB analyzer = new ArrayAnalyzerB(array);
        int result = analyzer.countInRange(minVal, maxVal);
        System.out.println("Count of Elements in Range: " + result);
    }
}
Leave a Comment