The*_*234 0 java loops for-loop
我需要制作一个看起来像这样的三角形
*
**
***
****
*****
******
*******
Run Code Online (Sandbox Code Playgroud)
目前我有一个看起来像的工作
*
**
***
****
*****
******
*******
Run Code Online (Sandbox Code Playgroud)
使用循环:
public static void standard(int n)
{
for(int x = 1; x <= n; x++)
{
for(int c = 1; c <= x; c++)
{
System.out.print("*");
}
System.out.println();
}
}
Run Code Online (Sandbox Code Playgroud)
我如何开展这项工作
*
**
***
****
*****
******
*******
Run Code Online (Sandbox Code Playgroud)
这是我的尝试:
public static void backward(int n)
{
for(int x = 7; x <= n; x++)
{
for(int y = 1; y >= x; y--)
{
if (x >= y)
{
System.out.print("*");
}
else
{
System.out.print("");
}
}
System.out.println();
}
}
Run Code Online (Sandbox Code Playgroud)
在每行打印n字符:如果索引c < n - x,打印空格,否则打印星号:
for (int x = 1; x <= n; x++) {
for (int c = 0; c < n; c++)
System.out.print(c < n - x ? ' ' : '*');
System.out.println();
}
Run Code Online (Sandbox Code Playgroud)
输出(n = 6):
*
**
***
****
*****
******
Run Code Online (Sandbox Code Playgroud)