我需要在Java中使用嵌套的for循环来制作这样的三角形
********
*******
******
*****
****
***
**
*
这是我的代码:
for (int i=8; i>0; i--)
{
for (int j=0; j<i; j++)
{
System.out.print('#');
}
System.out.println("");
}
我得到一个三角形,但不是我想要的三角形
您几乎明白了……您只是忘记了空格:
for (int i=8; i>0; i--) {
// print 8-i spaces, i.e. the complement number of spaces
for (int j=0; j<8-i; j++)
System.out.print(' ');
for (int j=0; j<i; j++)
System.out.print('#');
System.out.println("");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句