java - Java,需要帮助制作一个for循环来制作像带有单个字母的楼梯的图像
问题描述
我需要帮助来制作可以接受数字并打印该行数以制作图像的程序,并且它会像这样使用字母来表示图像。例如,我将输入数字 5,然后程序将打印这个.....
x
xx
xxx
xxxx
xxxxx
|
public class Stairs
{
public static void Stairs1 (int height)
{
for (int row = 0; row < height; row = row + 1)
{
for (int col = 0; col <= row; col = col + 1)
{
System.out.print("x");
}
System.out.println();
}
}
}
这是为了做到这一点。我正在尝试使事情变得更简单,但它会像这样从左侧翻转到右侧
......
x
xx
xxx
xxxx
xxxxx
解决方案
试试下面的东西;它应该为您提供所需的输出:
// Function to demonstrate printing pattern
public static void printStars(int n)
{
int i, j;
// outer loop to handle number of rows
// n in this case
for(i=0; i<n; i++)
{
// inner loop to handle number spaces
// values changing acc. to requirement
for(j=2*(n-i); j>=0; j--)
{
// printing spaces
System.out.print(" ");
}
// inner loop to handle number of columns
// values changing acc. to outer loop
for(j=0; j<=i; j++)
{
// printing stars
System.out.print("* ");
}
// ending line after each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printStars(n);
}
}
推荐阅读
- java - 如何使用 SunMSCAPI 密钥解密 RSA-OAEP
- r - pivot_wider 基于 0 或 1 的条件
- windows - Haskell,读取另一个进程的内存
- javascript - 需要帮助将回调转换为 Promise
- c# - 如何在使用 Linq 的特定 Capture.Index 之后从 MatchCollection 中获取下一个匹配项
- sql - ROUNDDOWN TIME, Power Bi
- for-loop - 如何使用 Dart 从 for 循环中返回值
- sql - 以降序和随机顺序计数 (*)
- java - 如何在同一台mac上处理多个版本的JDK
- c++ - Visual Studio 2019 不想为 LIB 版本生成 ASM 文件,我做错了什么?