0

[SOLVED] Summing elements in arrays issues

For some reason this code keeps skipping the first index and if the first number is a factor of 4 it wont add it to the sum import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] array = new int[length]; int sum=0; for (int i = 0; i < length; i++) { array[i] = scanner.nextInt(); if(array[i]%4==0) { sum=+array[i]; } } System.out.println(sum); //your code goes here } }

19th Jan 2021, 2:08 PM
Serana Zentha
Serana Zentha - avatar
10 Réponses
+ 7
Serana Zentha According to SoloLearn practice you can first store all the input in an array like this int[] array = new int[length]; for (int i = 0; i < length; i++) { array[i] = scanner.nextInt(); } Then you can calculate sum like this for (int i = 0; i < length; i++) { if(array[i] % 4 == 0) { sum += array[i]; } }
19th Jan 2021, 2:23 PM
A͢J
A͢J - avatar
+ 3
Serana Zentha First thing why you are taking input in array inside loop, second thing this =+ syntax is wrong. Here should be += Do like this: for (int i = 0; i < length; i++) { int item = scanner.nextInt(); if (item % 4 == 0) { sum += item; } }
19th Jan 2021, 2:13 PM
A͢J
A͢J - avatar
+ 1
Serana Zentha Here is complete code import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int item = 0; int sum = 0; for (int i = 0; i < length; i++) { item = scanner.nextInt(); if(item % 4 == 0) { sum += item; } } System.out.println(sum); } }
19th Jan 2021, 2:18 PM
A͢J
A͢J - avatar
+ 1
Thank you, but it s the weirdest practice so far
2nd Feb 2021, 8:47 AM
C Hri S Tatsios
C Hri S Tatsios - avatar
0
I Am AJ ! For some reason it game me an error last time when I did += because i didnt declare sum outside of the for loop. I also needed to print the sum only 8nce and putting it inside if the for loop or if statement would have kept repeating different intervals in sum
19th Jan 2021, 2:15 PM
Serana Zentha
Serana Zentha - avatar
0
I Am AJ ! Also for the input it wasnt me, its part of the practice coding Sololearn makes. Without it, however, the for loop wont work
19th Jan 2021, 2:16 PM
Serana Zentha
Serana Zentha - avatar
0
I Am AJ ! That also works but in some situations you may need the array index like in the code I was given. Yours does not require the array index as there is no array. I was able to fix my code thanks to your help, though
19th Jan 2021, 2:21 PM
Serana Zentha
Serana Zentha - avatar
0
Yeah I was able to do that, thanks so much!
19th Jan 2021, 2:25 PM
Serana Zentha
Serana Zentha - avatar
0
Serana Zentha Welcome. There are many ways to solve any problem.
19th Jan 2021, 2:26 PM
A͢J
A͢J - avatar
0
This exercise was very confusing at first due to the display of the result. The solution SL gave is actually correct, and it is totally acceptable to take input for the array inside the loop. In the results, the first set of numbers are merely the predefined inputs, NOT whats being displayed! The very first number is simply the first input, which represents the length of the array. Then the subsequent numbers each represent an element within the array. Count the number of numbers after the first one, and the first number makes sense. It was never supposed to be factored into the sum equation. The only displayed output to look out for is the final number, which is the ‘sum’ from the println. I think many of us overcomplicated the problem in our heads after seeing the answer 😂
11th May 2022, 3:38 PM
Songyue Huang
Songyue Huang - avatar