It is necessary to find the sum of the elements of the array after finding the first negative element and output all elements after the first negative element. For example, there is an array {0,2,-5,7,4,9,3,-2,5,4,5} you need to find the first negative number, here -5 , then print the sum of the remaining elements 7,4,9,3,-2,5,4,5} . I did not quite work out:
int[]arr = {0,2,-5,7,4,9,3,-2,5,4,5}; int firstNegative = 0; int countIndex = 0; int sum = 0; for (int i = 0; i < arr.length; i++) { countIndex++; if (arr[i] < 0) { firstNegative = arr[i]; for (int j = 0; j < (arr.length - countIndex); j++) { sum+=arr[j]; } break; } System.out.println(arr[i]); } System.out.println("My first: " + firstNegative); System.out.println("Sum is: " + sum);
sum+=arr[j]and how to count from the index where is the first negative number? - Nevada