Can't figure out how to get this just to print the sum. It prints the total but a bunch of other stuff too. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can't figure out how to get this just to print the sum. It prints the total but a bunch of other stuff too.

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int len = scanner.nextInt(); int[] arr = new int[len]; int sum = 0; for (int i = 0; i < len; i++) { arr[i] = scanner.nextInt(); if (arr[i] %4 ==0) sum = sum+arr[i]; { System.out.println(sum); { } } } } }

20th Mar 2021, 9:41 AM
Michael Irving
Michael Irving - avatar
4 Answers
0
The System.out.println is inside the for loop that is why you get an output for every array entry. Good formatted code is important to understand what is inside which braces.
20th Mar 2021, 9:51 AM
Dude
0
Your not understanding what nextInt() means. First time you use it the first number is stored in len which is used for the size of your array so you have hard coded your array to be of a specific size. After this I know what your code is doing but it dosent make sense. Learn more about ... Scanner objects nextInt() method converting Scanner inputs to Array Using += to store values to previous values Scopes & braces Array size using -> [size]
20th Mar 2021, 11:59 AM
D_Stark
D_Stark - avatar
0
Thanks guys I got it to work!
20th Mar 2021, 5:31 PM
Michael Irving
Michael Irving - avatar
0
This problem sems to be unsolvable!!
8th Mar 2022, 2:29 AM
Chris
Chris - avatar