Print Numbers with a loop from an array
Hi I am a total beginner for Java and I am here with a noob question.
I did the following code, so the numbers from 20 to 51 are printed. The numbers were printed, but the problem is that they got printed twice instead of once.
Why did this happen to me??
The following is the code:
int [] hold;
hold = new int[31];
int i;
for (i = 20; i < 51; i = i +1)
System.out.println( i );
Re: Print Numbers with a loop from an array
I'm not sure what you are doing with the hold[], but I just ran your code and the numbers only print once.
Also, per convention, with a for loop you would use i++, instead of i = i+1.
The code runs the same, its just a coding style
Code:
for(int i=20; i<51; i++){
}