Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static int inputLength() {
- Scanner scan = new Scanner(System.in);
- int length = 0;
- boolean isIncorrect;
- System.out.println("Enter the length of the array (from 1 to 20)");
- do {
- isIncorrect = false;
- try {
- length = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- System.out.println("Incorrect input!\nEnter the integer");
- isIncorrect = true;
- }
- if (!isIncorrect && (length < 1 || length > 20)) {
- System.out.println("Incorrect input!\nEnter the length of the range from 1 to 20");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return length;
- }
- public static void inputArray(double[] array) {
- Scanner scan = new Scanner(System.in);
- boolean isIncorrect;
- for (int i = 0; i < array.length; i++) {
- do {
- isIncorrect = false;
- System.out.println("Enter the array element at number " + (i + 1));
- try {
- array[i] = Double.parseDouble(scan.nextLine());
- } catch (Exception e) {
- System.out.println("Incorrect input!\nEnter the number");
- isIncorrect = true;
- }
- } while (isIncorrect);
- }
- }
- public static void printArray(double[] array) {
- System.out.println("Array: ");
- for (double element : array) {
- System.out.print(element + "; ");
- }
- System.out.println("\n");
- }
- public static double findSumOfOddElements(double[] array) {
- double sum = 0;
- for (int i = 0; i < array.length; i++) {
- if (i % 2 == 0) {
- sum += array[i];
- }
- }
- return sum;
- }
- public static int findIndexOfFirstNegative(double[] array) {
- int i = 0;
- int firstNegative = -1;
- boolean isNotFound = true;
- while (i < array.length && isNotFound) {
- if (array[i] < 0) {
- isNotFound = false;
- firstNegative = i;
- }
- i++;
- }
- return firstNegative;
- }
- public static int findIndexOfLastNegative(double[] array) {
- int i = array.length - 1;
- int lastNegative = -1;
- boolean isNotFound = true;
- while (i > 0 && isNotFound) {
- if (array[i] < 0) {
- isNotFound = false;
- lastNegative = i;
- }
- i--;
- }
- return lastNegative;
- }
- public static double findSumBetweenNegatives(double[] array, int firstNegative, int lastNegative) {
- int sum = 0;
- for (int i = firstNegative + 1; i < lastNegative; i++) {
- sum += array[i];
- }
- return sum;
- }
- public static void outputResult (double sumOfOddElements, double sumBetweenNegatives, int firstNegative, int lastNegative) {
- if (firstNegative != -1 || lastNegative != -1) {
- System.out.println("The sum of odd elements of the array is " + sumOfOddElements);
- System.out.println("The sum between the first and last negative array element is " + sumBetweenNegatives);
- } else {
- System.out.println("The sum of even elements of the array is " + sumOfOddElements);
- System.out.println("The array does not contain at least two negative elements");
- }
- }
- public static void main(String[] args) {
- int length = inputLength();
- double[] array = new double[length];
- inputArray(array);
- printArray(array);
- double sumOfOddElements = findSumOfOddElements(array);
- int firstNegative = findIndexOfFirstNegative(array);
- int lastNegative = findIndexOfLastNegative(array);
- double sumBetweenNegatives = findSumBetweenNegatives(array, firstNegative, lastNegative);
- outputResult(sumOfOddElements, sumBetweenNegatives, firstNegative, lastNegative);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement