所以我一直试图解决这个问题,但没有成功。所以,我想创建一个程序来打印出以下三角形。
**********
*********
********
*******
******
*****
****
***
**
*
这是我当前的代码。
public class PatternC
{
public static void main(String[] args)
{
int len = 1;
int k;
System.out.println("**********");
for (int row = 9; row > len; row--)
{
for(int i=0; i<row; i++)
System.out.print("*");
System.out.println("");
}
for (int end = 0; end<1; end++)
{
System.out.format("%10s", "*");
}
}
}
但是程序正在打印这个!
**********
*********
********
*******
******
*****
****
***
**
*
我做的一切都是正确的,除了我只需要对齐中间的星星以最初从右到左开始。我已经尝试过打印格式,但不起作用。
最佳答案
也许这些空间让你感到困惑。
如何打印以下图案?
**********
X*********
XX********
XXX*******
XXXX******
XXXXX*****
XXXXXX****
XXXXXXX***
XXXXXXXX**
XXXXXXXXX*
基本上是:
Print 0 X's, then 10 *'s
Print 1 X, then 9 *'s
Print 2 X's, then 8 *'s
.
.
.
Print 9 X's, then 1 *
一旦你得到了它,在你的代码中用空格字符替换“X”,你就得到了它。
关于Java从右到左打印语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26027046/