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); { } } } } }
4 ответов
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.
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]
0
Thanks guys I got it to work!
0
This problem sems to be unsolvable!!