本文介绍了关于星形图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试打印星形图案
I am trying to print below star pattern
*
***
*****
***
*
我使用以下逻辑打印:
*
***
*****
上半年代码:
int i, j;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= i; j++)
System.out.print("*");
for (j = i - 1; j >= 1; j--)
System.out.print("*");
System.out.println();
}
但我仍不确定如何打印整个结构。
But still I am not sure about how to print the whole structure.
推荐答案
你只需要反向编写循环,从upperBound开始 - 1.参见下面的代码:
You just have to write in reverse the loop, to start from the upperBound - 1. See the code bellow:
int numberOfLines = 3;
for (int i = 1; i <= numberOfLines; i++) {
for (int j = 1; j < 2*i; j++){
System.out.print("*");
}
System.out.println();
}
for (int i = numberOfLines - 1; i > 0; i--) {
for (int j = 1; j < 2*i; j++){
System.out.print("*");
}
System.out.println();
}
这篇关于关于星形图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!