Results 1 to 4 of 4
  1. #1
    s_mxyzptlk is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Help with Printing Array... Im supposed to print out so 9 elements show per line.

    I need a quicker way to do this... As you can see i used for loops for each 9 elements but it will take me forever considering I have to do this about 4 times :/

    public static void main(String[] args) {
    int[] A = new int[123]; // array will store 123 values
    int d = 0;
    int swap = 0; // swap counter
    int compare = 0; // compare counter

    Random generator = new Random(2020); // creates instance of Random class

    for (int i = 0; i < A.length; i++) {
    d = generator.nextInt(999); // Maximum value declared is 999
    A[i] = d;

    }
    for (int i=0; i<9; i++){
    System.out.print(A[i] + " "); // Prints array of values generated by Random class
    }
    System.out.println("");
    for (int i=10; i<19; i++){
    System.out.print(A[i] + " "); // Prints array of values generated by Random class
    }
    System.out.println("");
    for (int i=20; i<29; i++){
    System.out.print(A[i] + " "); // Prints array of values generated by Random class
    }
    System.out.println("");
    for (int i=30; i<39; i++){
    System.out.print(A[i] + " "); // Prints array of values generated by Random class
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Help with Printing Array... Im supposed to print out so 9 elements show per line.

    Did you have a question? Simply posting unformatted code does not make it easy to help you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    s_mxyzptlk is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help with Printing Array... Im supposed to print out so 9 elements show per line.

    Yeah I included it in title and before program... Theres 123 elements in the array and I have to print it out so that 9 elements are on each line... With the last row being less than nine of course... I need a quicker way to do it rather than what Im doing now

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Help with Printing Array... Im supposed to print out so 9 elements show per line.

    I will just point out that you have still not asked a question- usually questions end in a question mark. You have simply said what you need, which is not a question.

    Anyway, you might want to read up on the % operator.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 2
    Last Post: 12-02-2010, 04:18 AM
  2. printing 3 elements in an array on 1 line
    By Symbiot in forum New To Java
    Replies: 2
    Last Post: 05-27-2010, 12:29 PM
  3. Replies: 3
    Last Post: 03-22-2010, 01:50 AM
  4. Replies: 0
    Last Post: 03-10-2010, 02:13 AM
  5. Printing Array elements
    By new person in forum New To Java
    Replies: 2
    Last Post: 02-23-2009, 08:57 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
  •