Results 1 to 12 of 12

Thread: Quick question.

  1. #1
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Quick question.

    Hi guys, I have a quick question. (Sorry, I'm not very good at for-loops)

    In this code:

    int value [] = {0};

    for (int counter =0; counter < value.length; counter++){
    System.out.print(counter + value[counter]);
    System.out.print(counter + value[counter]);
    System.out.print(counter + value[counter]);
    System.out.print(counter + value[counter]);
    System.out.print("\n");
    }


    It prints out 0000. I want it to repeat it again on the next line. Is there an efficient way of doing it?
    Thanks.

  2. #2
    CodeAdmiral is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Quick question.

    try changing each System.out.print to System.out.println (LN)
    That should create a new line each time an output occurs.

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

    Default Re: Quick question.

    Word to the wise- use more descriptive titles.

    What exactly do you want it to repeat on the next line? Another four zeroes? Or each zero on its own line? And what do you mean by efficient?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Quick question.

    Quote Originally Posted by KevinWorkman View Post
    Word to the wise- use more descriptive titles.

    What exactly do you want it to repeat on the next line? Another four zeroes? Or each zero on its own line? And what do you mean by efficient?
    Yeah, another four zeros. Sorry about that.

  5. #5
    CodeAdmiral is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Quick question.

    My example will make a 0 on each line.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Quick question.

    Put a loop inside a loop. The outer loop will produce the inner loop n times. Here is an example

    Java Code:
    for (int i = 0; i < 10; ++i){
      for (int i = 0; i < someLength; ++i){
        do something
      }
    }

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Quick question.

    .........
    Last edited by al_Marshy_1981; 12-08-2011 at 06:47 PM. Reason: needs 4 0s

  8. #8
    CodeAdmiral is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Quick question.

    Then make a loop with what you want repeated inside.

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Quick question.

    I think a ternary if inside a System.out.print might get it down to one line in the for loop.

  10. #10
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Quick question.

    Quote Originally Posted by sunde887 View Post
    Put a loop inside a loop. The outer loop will produce the inner loop n times. Here is an example

    Java Code:
    for (int i = 0; i < 10; ++i){
      for (int i = 0; i < someLength; ++i){
        do something
      }
    }
    With the letter 'i', wouldn't that be 'counter' from my code??

  11. #11
    juxta is offline Member
    Join Date
    Nov 2011
    Posts
    30
    Rep Power
    0

    Default Re: Quick question.

    Quote Originally Posted by juxta View Post
    With the letter 'i', wouldn't that be 'counter' from my code??
    Ignore that. Thanks sunde

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Quick question.

    Quote Originally Posted by juxta View Post
    int value [] = {0};
    Using an array to store a single element is pointless.

Similar Threads

  1. Quick question
    By Qsc in forum New To Java
    Replies: 6
    Last Post: 07-25-2011, 04:36 AM
  2. Really Quick question
    By Ryan10 in forum New To Java
    Replies: 63
    Last Post: 03-02-2011, 02:11 AM
  3. Quick Question...
    By FatalSylence in forum New To Java
    Replies: 4
    Last Post: 10-15-2010, 02:38 PM
  4. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:53 AM
  5. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 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
  •