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?
6 replies to this topic
#1
Posted 16 January 2011 - 11:12 AM
|
|
|
#2
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"
for (int pl = 0; pl <= j; pl++)
//echo "pl"
#3
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.)
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
Posted 17 January 2011 - 02:49 PM
This works:
But I assume you already done it :)
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
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
What adjustments do i make to get rid of the the empty space on the first line out output to the console?
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
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]
}
}
}
#7
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.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top










