Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By ritesh_java

Thread: How to use a for loop for this?

  1. #1
    bronzefish is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default How to use a for loop for this?

    Java Code:
    String[] suits = {"Mr", "Mrs", "Miss", "Master" }; //array of strings
    String[] ranks = {"A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M"}; //array of strings
    String[] job ={ " the Footballer", " the Cricketer"," the Motorist", " the Announcer", " the Boxer", " the Golfer"," the Boatman", " the Photographer", " the Weight Lifter"," the Runner", " the Gardener","the Gamer", "the TechGuy"};
    
    String[] title ={"", " 's wife", " 's daughter"," 's son"};
    
    public static void DeckCreate(){
    String[] arrayCards = new String[47];
    ===============================
    then the loop codes will be here
    my result when i print it should be the following:

    Mr A, the Footballer
    Mrs A, the Footballer's wife
    Miss A, the Footballer's daughter
    Master A, the Footballer's son
    Mr B, the Cricketer
    Mrs B, the Cricketer's wife
    and so on...
    =====================
    can anyone explain and show to me on how to loop into the array for this? i need references. i am sure that for loop is used, i tried but it didn't turn out very well. i am just one step away to typing all the names out in the array. any help will be appreciated. THANKS!

    I think i post it in the right section, forgive me if it is not.I am new to java and this forum. Thanks again

    i am not supposed to use 2d-3d array, arraylist or use any GUI.
    Last edited by bronzefish; 12-02-2012 at 08:31 AM.

  2. #2
    ritesh_java is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Re: How to use a for loop for this?

    Thats very simple you have to use only two for loop's

    Java Code:
    for(int i=0;i<ranks.length;i++)
    		{
    			for(int j=0;j<suits.length;j++)
    			{
    				System.out.println(suits[j]+" "+ranks[i]+", "+job[i]+title[j]);
    			}
    		}
    and this will give u desired output.

    Mr A, the Footballer
    Mrs A, the Footballer's wife
    Miss A, the Footballer's daughter
    Master A, the Footballer's son
    Mr B, the Cricketer
    Mrs B, the Cricketer's wife
    Miss B, the Cricketer's daughter
    Master B, the Cricketer's son
    Mr C, the Motorist
    Mrs C, the Motorist's wife
    Miss C, the Motorist's daughter
    Master C, the Motorist's son
    Mr D, the Announcer
    Mrs D, the Announcer's wife
    Miss D, the Announcer's daughter
    Master D, the Announcer's son
    ..........
    And please like it if it helps you.
    Last edited by ritesh_java; 12-02-2012 at 08:35 AM.
    bronzefish likes this.

  3. #3
    bronzefish is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: How to use a for loop for this?

    Thanks, really, you saved me. One question, what do you suggest if i want to put the output into an array as i need to shuffle it then distribute? because this should be a card game.

  4. #4
    ritesh_java is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Re: How to use a for loop for this?

    If you want to put the output into an array then why dont u just keep these string into array instead of creating 4 array earlier and then creating pattern from these and then again making one more array.

Similar Threads

  1. Converting a for loop to a do-while loop
    By awesom in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 03:02 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  5. Replies: 5
    Last Post: 04-30-2010, 05:05 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
  •