Java Practice It || 2.16 SlashFigure || nested for loops

preview_player
Показать описание
Question:
Write a Java program in a class named SlashFigure that produces the following output. Use nested for loops to capture the structure of the figure. (If you previously solved Self-Check problems 34 and 35 in the book, you will have created a loop table that will be useful in solving this problem. Use it!)
!!!!!!!!!!!!!!!!!!!!!!
\\!!!!!!!!!!!!!!!!!!//
\\\\!!!!!!!!!!!!!!////
\\\\\\!!!!!!!!!!//////
\\\\\\\\!!!!!!////////
\\\\\\\\\\!!//////////

Problem:
Рекомендации по теме
Комментарии
Автор

Here is how I solved it:

public class SlashFigure {
public static void main(String[] args) {
for (int k=1; k<=6; k++) {
System.out.println();

for (int j=1; j<=k-1; j++) { // back slash(\) for loop
System.out.print("\\\\");
}
for (int i=26-k*4; i>=1; i--) { // ! for loop
System.out.print("!");
}
for (int a=1; a<=k-1; a++) { // forward slash(/) for loop
System.out.print("//");
}

}
}
}

TheFifthofFive
Автор

question could you explain why a*2 was needed? thanks!

demtix
welcome to shbcf.ru