Jump to content

Need with with for nested loops

- - - - -

  • Please log in to reply
6 replies to this topic

#1
bkim33

bkim33

    Newbie

  • Members
  • Pip
  • 4 posts
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?

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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"

#3
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
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

    }

}




}


#4
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
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 :)

#5
bkim33

bkim33

    Newbie

  • Members
  • Pip
  • 4 posts
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?

#6
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
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]
			}
				
		}
	}


#7
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users