Java program Write a method called printDesign that produces the following outpu
ID: 3710649 • Letter: J
Question
Java program
Write a method called printDesign that produces the following output using nested for loops to print out the structure.
Your method should look like the following.
Note that in the example above height = 6 AND it prints 6 rows. If I change height to another number, it should then print a different number of rows. Your program should adjust itself based upon the value of height.
For example... if I change it so that
it should print
But... if I change it so that
it should print
Notes:
There are 9 '-''s in the first row.
On line 10 it only prints the 0, not 10
On line 11 it prints 1 not 11
After line 9 it goes off the edge, this is normal.
Explanation / Answer
PrintDesignTest.java
public class PrintDesignTest {
public static void main(String[] args) {
printDesign(12);
}
public static void printDesign(int height){
for (int i = 1,x=1; i < height * 2; i += 2,x++) {
int n = x % 10;
for (int j = 0; j < height* 2 - 1 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print(n);
System.out.print(" ");
}
}
}
Output:
1
222
33333
4444444
555555555
66666666666
7777777777777
888888888888888
99999999999999999
0000000000000000000
111111111111111111111
22222222222222222222222
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.