+ 1
I am taking values and store in array whats wrong with my code
public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[]=sc.nextInt(); } for(int n:a) { System.out.println (n); } } }
2 Answers
+ 1
1. you are declaring n twice... I think that's not possible
2. if you want to assign a value to an array, you have to know what element you want to set. So you have to put an index into the brackets of a[]... so the line "a[] = sc.nextInt();" is wrong.
In your case you should put i into the brackets and use another letter for the variable in your second loop.
0
please give correct code