Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Nested loops

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Nested loops

    I need to use a nested loop to print the following output but an unsure how to do it with the two zeros printing after the 9. Any help appreciated. Need to print:
    1122334455667788990011223344556677889900

    Thanks. This is a method that I wrote, but I had to print the zeros as literals in a separate print statement. I know that there must be a better algorithm.

    public static void drawNumbers(){
    		for (int line = 1; line <= 2; line++){
    			for (int i =1; i <= 9; i++){
    				for (int j =1; j<=2; j++){
    					System.out.print(i);
    				}
    			}	
    		System.out.print("00");
    		}
    	}
    Last edited by Rookie; April 29th, 2014 at 07:49 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Nested loops

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Show what you've tried or can do with/without the zeros.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nested loops

    I posted my code above. Any help on improving appreciated.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Nested loops

    I don't see any comments in the code that describes the steps the code is trying to take to solve the problem.
    Can you add some comments saying what the different parts of the code are trying to do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Nested loops

    Have you heard of the remainder or modulus operator? I think that combined with one loop will accomplish the task.

  6. #6
    Junior Member
    Join Date
    May 2014
    Posts
    2
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nested loops

    how about this:
    for 1 to 10
    ....
    print i%10

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Nested loops

    ** code removed
    are u satisfy with this???
    Last edited by rita khatei; May 14th, 2014 at 03:19 PM. Reason: Please don't do OPs work for him

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Nested loops

    @ rita khatei, please don't do the OP's work for them. Rather, push them in the right direction. Your code has been removed based upon forum policy - edit it again and further action will be taken.

Similar Threads

  1. Re: Nested for loops
    By capt_jaggy in forum Loops & Control Statements
    Replies: 5
    Last Post: March 18th, 2013, 05:21 AM
  2. Nested For Loops
    By javapol in forum Java Theory & Questions
    Replies: 10
    Last Post: February 22nd, 2013, 10:46 PM
  3. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 11:43 PM
  4. Using Nested Loops
    By m2msucks in forum Loops & Control Statements
    Replies: 7
    Last Post: November 5th, 2011, 07:05 PM
  5. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM