Advertisement
Vanya_Shestakov

SumsArray

Nov 1st, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static int inputLength() {
  7.         Scanner scan = new Scanner(System.in);
  8.         int length = 0;
  9.         boolean isIncorrect;
  10.         System.out.println("Enter the length of the array (from 1 to 20)");
  11.         do {
  12.             isIncorrect = false;
  13.             try {
  14.                 length = Integer.parseInt(scan.nextLine());
  15.             } catch (Exception e) {
  16.                 System.out.println("Incorrect input!\nEnter the integer");
  17.                 isIncorrect = true;
  18.             }
  19.             if (!isIncorrect && (length < 1 || length > 20)) {
  20.                 System.out.println("Incorrect input!\nEnter the length of the range from 1 to 20");
  21.                 isIncorrect = true;
  22.             }
  23.         } while (isIncorrect);
  24.         return length;
  25.     }
  26.  
  27.     public static void inputArray(double[] array) {
  28.         Scanner scan = new Scanner(System.in);
  29.         boolean isIncorrect;
  30.  
  31.         for (int i = 0; i < array.length; i++) {
  32.             do {
  33.                 isIncorrect = false;
  34.                 System.out.println("Enter the array element at number " + (i + 1));
  35.                 try {
  36.                     array[i] = Double.parseDouble(scan.nextLine());
  37.                 } catch (Exception e) {
  38.                     System.out.println("Incorrect input!\nEnter the number");
  39.                     isIncorrect = true;
  40.                 }
  41.             } while (isIncorrect);
  42.         }
  43.     }
  44.  
  45.     public static void printArray(double[] array) {
  46.         System.out.println("Array: ");
  47.         for (double element : array) {
  48.             System.out.print(element + "; ");
  49.         }
  50.         System.out.println("\n");
  51.     }
  52.  
  53.     public static double findSumOfOddElements(double[] array) {
  54.         double sum = 0;
  55.         for (int i = 0; i < array.length; i++) {
  56.             if (i % 2 == 0) {
  57.                 sum += array[i];
  58.             }
  59.         }
  60.         return sum;
  61.     }
  62.  
  63.     public static int findIndexOfFirstNegative(double[] array) {
  64.         int i = 0;
  65.         int firstNegative = -1;
  66.         boolean isNotFound = true;
  67.         while (i < array.length && isNotFound) {
  68.             if (array[i] < 0) {
  69.                 isNotFound = false;
  70.                 firstNegative = i;
  71.             }
  72.             i++;
  73.         }
  74.         return firstNegative;
  75.     }
  76.  
  77.     public static int findIndexOfLastNegative(double[] array) {
  78.         int i = array.length - 1;
  79.         int lastNegative = -1;
  80.         boolean isNotFound = true;
  81.         while (i > 0 && isNotFound) {
  82.             if (array[i] < 0) {
  83.                 isNotFound = false;
  84.                 lastNegative = i;
  85.             }
  86.             i--;
  87.         }
  88.         return lastNegative;
  89.     }
  90.  
  91.     public static double findSumBetweenNegatives(double[] array, int firstNegative, int lastNegative) {
  92.         int sum = 0;
  93.         for (int i = firstNegative + 1; i < lastNegative; i++) {
  94.             sum += array[i];
  95.         }
  96.         return sum;
  97.     }
  98.  
  99.     public static void outputResult (double sumOfOddElements,  double sumBetweenNegatives, int firstNegative, int lastNegative) {
  100.         if (firstNegative != -1 || lastNegative != -1) {
  101.             System.out.println("The sum of odd elements of the array is " + sumOfOddElements);
  102.             System.out.println("The sum between the first and last negative array element is " + sumBetweenNegatives);
  103.         } else {
  104.             System.out.println("The sum of even elements of the array is " + sumOfOddElements);
  105.             System.out.println("The array does not contain at least two negative elements");
  106.         }
  107.     }
  108.  
  109.     public static void main(String[] args) {
  110.         int length = inputLength();
  111.         double[] array = new double[length];
  112.         inputArray(array);
  113.         printArray(array);
  114.         double sumOfOddElements = findSumOfOddElements(array);
  115.         int firstNegative = findIndexOfFirstNegative(array);
  116.         int lastNegative = findIndexOfLastNegative(array);
  117.         double sumBetweenNegatives = findSumBetweenNegatives(array, firstNegative, lastNegative);
  118.         outputResult(sumOfOddElements, sumBetweenNegatives, firstNegative, lastNegative);
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement