Results 1 to 4 of 4
  1. #1
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default for loops logic help

    I need help with a bit of logic on a for loop.

    Lets say I have an array that I want to add 20 ImageIcons to. thats easy enough:

    (i obviously have my gif files named 1.gif, 2.gif, etc....)
    Java Code:
    for(int i=0; i<20; i++){
                int j = i+1;
                cardIcon[i] = new ImageIcon("assets/cards/"+j+".gif");
            }
    Now here is where I am stumped.

    I want to do basically that same thing, only the ImageIcons have descriptions that work in pairs.

    Java Code:
    cardIcon[0]= new ImageIcon ("assets/cards/1.gif", "1");
    cardIcon[1]= new ImageIcon ("assets/cards/2.gif", "1");
    cardIcon[2]= new ImageIcon ("assets/cards/3.gif", "2");
    cardIcon[3]= new ImageIcon ("assets/cards/4.gif", "2");
    and so on.....

    The pair relationship of the descriptions is important.

    How could I set up my loop to increment the description every other time?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    thank you.

    ill give it a shot right now

  4. #4
    scheffetz is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    yeah that worked. I had to add 1 extra step to it.

    Java Code:
    for(int i=0; i<20; i++){
                int j = 1+i;
                int k = 1+(i/2);
                String temp = (""+k);
                cardIcon[i] = new ImageIcon("assets/cards/"+j+".gif", temp);
            }

Similar Threads

  1. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 06:44 PM
  2. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  3. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 11:06 AM
  4. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 02:18 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 AM

Posting Permissions

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