Reputation: 929
I'm trying to store odd numbers in an array, but when I run the code I'm getting 9
, five times as answer. It's only storing the value 9
.
Here are the codes:
public class Number2 {
public static void main(String[] args) {
int[] element = new int[5];
for(int i=0; i<5; i++) {
for(int j=1; j <= 10; j=j+2) {
element[i] = j;
}
}
for(int i=0; i < 5; i++) {
System.out.println(element[i]);
}
}
}
Can you tell me what's wrong with my program?
Upvotes: 1
Views: 1851
Reputation: 1
Just use another layer of for loop
int elements[] = new int[5];
for (int i=0,j=1; j<=10; i++, j=j+2 )
{
elements[i] = j;
}
for (int i=0; i<5 ; i++ )
{
System.out.println(elements[i]);
}
Upvotes: 0
Reputation: 7
you have two loops, j loop is inner code of i loop which is running for 5 times as the coded of i loop, for i=0, j will run j=1,2,3,4,5,6,7,8,9 and than 9 will store in j[0] next for i=1, j=1,2,3,4,5,6,7,8,9 and than 9 will store in j[1], and so on, so you will have to change
int j=1;
for(int i=0;i<5;i++){
element[i]=j;
j=j+2;
}
and for printing you have do only
for(int i=0;i<5;i++){
System.out.println(element[i]);
}
Upvotes: 0
Reputation: 3106
You use two loops, where you would need only one. Currently for every element in your array, you set it to 1, then to 3, then to 5, then to 7, then to 9, overwriting the former value each time. Thus in the end, your array is just {9,9,9,9,9}
. You probably wanted to do this:
for(int i = 0; i < element.length; i++) {
element[i] = i*2 + 1;
}
Upvotes: 7
Reputation: 1583
public static void main(String[] args) {
int[] element = new int[5];
for (int i = 0; i < element.length; i++) {
element[i]=2*i+1;
}
for(int i : element){
System.out.println(i);
}
}
Upvotes: 0
Reputation: 915
You are replacing value of array in each iteration i by 9 (1,3,5,7,9 respectively)
Try Below
for(int i=0; i<5; ){
for(int j=1; j <= 10; j=j+2,i++){
element[i] = j;
}
}
You can also write this in one for loop
for(int j=1,i=0 ; j <= 10 && i<5; j=j+2,i++){
element[i] = j;
}
Output
Upvotes: 1
Reputation: 418585
The internal loop (with variable j
) runs from 1
up to 9
for each iteration of the outer index loop, and always assigns value to element[i]
hence each value of the element
array will be 9
.
Following your logic this is how it should be:
for (int i = 0, j = 1; i < 5; i++, j += 2)
element[i] = j;
You want to increment the value to be set along with the index variable.
A few improvements:
Since element
is an array not just an element, you should name it plural, like elements
. Also don't repeat the array length in multiple places in your code, an array knows its length: elements.length
, use that in the for
statement:
int[] elements = new int[5];
for (int i = 0, j = 1; i < elements.length; i++, j += 2)
elements[i] = j;
Also you don't really need 2 variables to do what you want, the i
th odd number can be calculated based on the index:
for (int i = 0; i < elements.length; i++)
elements[i] = i*2 + 1;
Upvotes: 2
Reputation: 2188
When the last time inner loop execute value of j = 9
and when j
become 11
it exits. You are storing each value of j
in the same index. ie. you are overwriting the element[i]
each time in the inner loop and i
doesn't change in the inner loop. So only the final value of j
is available.
Below code will give the first five odd numbers ie (1, 3, 5, 7, 9)
for(int i=0,j=1; i <= 4; j=j+2,i++)
{
element[i] = j;
}
Upvotes: 1
Reputation: 4763
You are changing all the elements in your array to the last value of j.
Your loop needs to be :
for (int i=0;i<5;i++)
{
elements[i]=2*i +1;
}
Think about it this way :
When i is 0 , then you are assigning to elements[0] values from 1 to 9 (the j loop). The last value assigned is 9.
Upvotes: 1