Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

need help with homework

nested loop

  • Please log in to reply
4 replies to this topic

#1 senor91

senor91

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 22 February 2011 - 12:40 AM

so i need some assistance in completing the code, i believe im a few steps away from finishing the code. i understand there are two nested loops, i however am having trouble for the last loop. the output i am looking for looks like this:

12345
23451
34512
45123
51234


all help is appreciated
[ATTACH]3731[/ATTACH]

edit: im not sure how to properly post the code on here as i have seen in other threads

Attached Files


  • 0

#2 cdg10620

cdg10620

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 344 posts
  • Programming Language:C#, JavaScript, PL/SQL, Transact-SQL, VBScript, Others

Posted 22 February 2011 - 06:03 AM

Take your code and put it inside
 tags.

As for your PrintSquare method, you need to change the second for loop like this:


	public static void printSquare(int min, int max)
	{
		for (int line=0; line<max-min + 1; line++)
		{
			for (int a=min+line; a<=max; a++)
			{
				System.out.print(a);
			}
			[B][COLOR="red"]for (int b=min; b<min+line; b++)
			{
				System.out.print(b);
			}[/COLOR][/B]
			System.out.println();
		}
	}

That fixes your issue. :)
  • 0
-CDG10620
Software Developer

#3 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 22 February 2011 - 08:49 AM

Here it is with 1 inner loop. Possibly a bit harder to undertand tho:
        for (int i = 0; i < 5 ; i++) {
            for (int j = 0 ; j < 5; j++) {
                System.out.print(1+(j+i)%5);
            }
            System.out.println();
        }

  • 0

#4 senor91

senor91

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 22 February 2011 - 09:43 AM

Thanks for the help, but i seem to be getting the output of:
123451
234512
345123
451234
512345

which is close, very close now but how would i start the second for loop on the second line?
  • 0

#5 cdg10620

cdg10620

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 344 posts
  • Programming Language:C#, JavaScript, PL/SQL, Transact-SQL, VBScript, Others

Posted 22 February 2011 - 07:49 PM

The loop that wim DC posted works perfectly...
  • 0
-CDG10620
Software Developer





Also tagged with one or more of these keywords: nested loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download