Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need with with for nested loops

nested loop loop

  • Please log in to reply
6 replies to this topic

#1 bkim33

bkim33

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 16 January 2011 - 11:12 AM

Hi I'm new to this forum and have found this site very useful!

The question I have is how do i write code so that my output will be the following?

999999999888888887777777666666555554444333221
999999999888888887777777666666555554444333221
999999999888888887777777666666555554444333221
999999999888888887777777666666555554444333221

For my outer loop I have:
for (int line = 1; line <= 4; line++)

System.out.println()

I know that this code counts down from 9

for (int j = 9; j >=1; j--)

what do I need to do to make each integer repeat itself x amount of times. x being the integers current value?
  • 0

#2 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 16 January 2011 - 12:12 PM

You need a third loop. After this do a new line. The third loop should loop from 1 to the number it's on and repeat the number that many times.

for (int pl = 0; pl <= j; pl++)
//echo "pl"
  • 0

#3 Skippy

Skippy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 134 posts

Posted 16 January 2011 - 01:16 PM

your going to want to break it down from the largest thing you are doing to the smallest thing.

the largest scope is the whole output. that is the outside.

the next largest scope is each line (you have this correct. thats the for loop of each line that needs to be repeated)
the next largest scope is each group

then each letter. (print statement.)

functionPrintPattern(){ // the largest scope

for each line // next largest scope
{
    for each group // next largest scope (group of numbers 9, 8, 7, 6, 5...)
    {
        for count in group // smallest scope, print amount of letters in group
            print letter
    }
}



}

  • 0

#4 Serialcek

Serialcek

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 151 posts

Posted 17 January 2011 - 02:49 PM

This works:

public class Print {

	public static void main(String[] args) {

		for (int line = 1; line <= 4; line++) {
			System.out.println();
			for (int j = 9; j >= 1; j--) {
				int counter = j;
				while (counter != 0) {
					System.out.print(j);
					counter--;
				}

			}

		}

	}

}

But I assume you already done it :)
  • 0

#5 bkim33

bkim33

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 17 January 2011 - 03:25 PM

Thanks for all of your help i figure out how to do it but have one issue, the first line is a blank space followed by the 99999999988888887777777


public static void main(String[ ] args) {
		for (int line = 1; line <= 4; line++) {
			System.out.println();
			for (int j = 9; j >= 1; j--) {
				for (int k = 1; k <= j; k++)
				System.out.print(j);
				}
			}
				
		}
	}

			
		


What adjustments do i make to get rid of the the empty space on the first line out output to the console?
  • 0

#6 Xdawn90

Xdawn90

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 53 posts
  • Learning:C, Java, C++, PHP, JavaScript, Visual Basic .NET

Posted 17 January 2011 - 07:28 PM

public static void main(String[ ] args) {
		for (int line = 1; line <= 4; line++) {
[COLOR=red]                        System.out.println();[/COLOR]
			for (int j = 9; j >= 1; j--) {
				for (int k = 1; k <= j; k++)
				System.out.print(j);
				}
			}
				
		}
	}

change to

public static void main(String[ ] args) {
		for (int line = 1; line <= 4; line++) {
			for (int j = 9; j >= 1; j--) {
				for (int k = 1; k <= j; k++)
				System.out.print(j);
				}
                                [COLOR=red]System.out.println();[/COLOR]
			}
				
		}
	}

  • 0

#7 sam_l

sam_l

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 52 posts

Posted 19 January 2011 - 03:06 AM

I think this indicates that you need to think more about the problem at hand. Try thinking it through and planning it out prior to sitting down at the keyboard and it can help you save a significant amount of time in the long run.
  • 0





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

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