+ 1

Sum in array

Hi everyone! I'm begginer here. How to write the sum of elements by user input. Using java

28th Dec 2022, 5:02 PM
Aenul
Aenul - avatar
7 Respuestas
+ 2
Learn about taking user input. Learn about loops.. Learn about arrays.. Then give an attempt to solve it first your own way. If not work then, ask community by sharing your attempt.. Hope you understand..
28th Dec 2022, 5:23 PM
Jayakrishna 🇮🇳
+ 2
Good try.. Some mistakes: nombor is not declared variable. First declare it then use it. int nombor = input.nextInt() ; next int [] nombor ; here variable nombor is already declared variable, use any other name. And you must initialize size of array and next assign or input values before using in for loop. Like int numA[] = { 1,2,3,4,5 }; Or by input in loop as for( int i = 0; i<numA.length; i++ ) numA[i] = input.nextInt(); Now you can enter values and use in loop. Hope it helps...
29th Dec 2022, 8:47 AM
Jayakrishna 🇮🇳
+ 2
You're welcome...
30th Dec 2022, 3:57 PM
Jayakrishna 🇮🇳
+ 1
I already did it, and it shows some errors
29th Dec 2022, 1:24 AM
Aenul
Aenul - avatar
+ 1
import java.util.Scanner; public class Array3 { public static void main(String[] args) { Scanner input =new Scanner(System.in); System.out.println("Enter the element you want to sum"); nombor = input.nextInt(); int[] nombor; int jumlah = 0; for (int i : nombor) { jumlah += i; } int arrayLength = nombor.length; System.out.println("Sum = " + jumlah); } }
29th Dec 2022, 1:25 AM
Aenul
Aenul - avatar
+ 1
Errors in lines 9 and 15
29th Dec 2022, 1:25 AM
Aenul
Aenul - avatar
+ 1
Thank you so muchhh♥️♥️ really work then !!
30th Dec 2022, 2:58 PM
Aenul
Aenul - avatar