+ 1
Sum in array
Hi everyone! I'm begginer here. How to write the sum of elements by user input. Using java
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..
+ 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...
+ 2
You're welcome...
+ 1
I already did it, and it shows some errors
+ 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);
}
}
+ 1
Errors in lines 9 and 15
+ 1
Thank you so muchhh♥️♥️ really work then !!