0

For Each loop not working as expected.

I am trying to create a new array and populate it using the for each. then I ask it to output the values. The input is not working. Do you know what's wrong? public class Program { public static void main(String[] args) { int[ ] primes; primes = new int[7]; for (int i: primes) { primes[i] = i; } for (int t: primes) { System.out.println(t); } } }

6th Jul 2016, 7:11 AM
Dakotah
2 ответов
+ 1
You'll want to replace the for each loop used to write content into the array with a normal for loop instead. The for each loop is used to iterate over an array of known elements and since you didn't add any values to it, each element in the array is given a default value of 0 (since its an array of int's)
6th Jul 2016, 9:32 AM
Triniel
0
Accept the input from the user using scanner class and create its object named sc and display that u want to accept input by statement System.out.println() and use a for loop to accept the prime nos...like... for(i=0;i<7;i++) {primes[i]=sc.nextInt(); } and then output the prime nos from the set of random nos given by the user..
8th Jul 2016, 4:59 PM
K.p.