Results 1 to 2 of 2
  1. #1
    ejdrijin is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default 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 );

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    354
    Rep Power
    5

    Default 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
    Java Code:
    for(int i=20; i<51; i++){
    
    }
    Last edited by sehudson; 05-30-2012 at 07:49 PM.

Similar Threads

  1. How do I print numbers in a line?
    By Emtino in forum New To Java
    Replies: 2
    Last Post: 04-17-2012, 08:08 AM
  2. Using enhanced for loop to print array list??
    By bdl1127 in forum New To Java
    Replies: 1
    Last Post: 02-19-2012, 12:43 AM
  3. Replies: 6
    Last Post: 12-08-2010, 12:39 AM
  4. print random numbers without repetition
    By princess.blue in forum New To Java
    Replies: 3
    Last Post: 02-04-2010, 09:37 AM
  5. how to multiply numbers in rows and print it next to it
    By racewithferrari in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 06:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •