Modify the SlashFigure program from the previous exercise to produce a new progr
ID: 3527109 • Letter: M
Question
Modify theSlashFigureprogram from the previous exercise to produce a new programSlashFigure2that uses a global constant for the figure's height. The previous output used a constant height of 6. Here is the outputs for a constant height of 4 and 7 respectively:
(You must solve this problem using only ONEpublic static finalconstant, not multiple constants; and its value must be usedin the waydescribed in this problem.)
!!!!!!!!!!!!!! \!!!!!!!!!!// \\!!!!!!//// \\\!!//////
!!!!!!!!!!!!!!!!!!!!!!!!!! \!!!!!!!!!!!!!!!!!!!!!!// \\!!!!!!!!!!!!!!!!!!//// \\\!!!!!!!!!!!!!!////// \\\\!!!!!!!!!!//////// \\\\\!!!!!!////////// \\\\\\!!////////////
Explanation / Answer
Break it down.
You have 6 lines that have 22 characters each.
1st iteration: 22 ! i = 0
2nd iteration: 2 /, 18 !, 2 / i = 1
3rd iteration: 4 /, 14 !, 4 / i = 2
4th iteration: 6 /, 10 !, 6 / i = 3
5th iteration: 8 /, 6 !, 8 / i = 4
6th iteration: 10 /, 2 !, 10 / i = 5
Find the pattern between i and the amount of '/' and '!'
It looks like the number of '/' before and after the '!' is i * 2. And the number of '!' is 22 - the amount of '/'
Now we have all of the information needed to make our loops
for (int i = 0; i < 6; i++) {
//print '/' before '!'
for (int j = 0; j < i*2; j++) {
System.out.print("/");
}
//print all '!'
for (int j = 0; j < 22 - (i*2); j++) {
System.out.print("!");
}
//print all '/' after '!'
for (int j = 0; j < i*2; j++) {
System.out.print("/");
}
//advance to next line
System.out.print(" );
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.