在 Java 中打印三角形图案
以下是打印三角形图案的 Java 程序 -
示例
import java.util.*;
public class Demo{
public static void main(String[] args){
Scanner my_scan = new Scanner(System.in);
System.out.println("Enter the number of rows which needs to be printed");
int my_row = my_scan.nextInt();
for (int i = 1; i <= my_row; i++){
for (int j = my_row; j >= i; j--){
System.out.print(" ");
}
for (int j = 1; j <= i; j++){
System.out.print("^ ");
}
System.out.println();
}
}
}需要在标准输入中输入内容 - 5
输出
Enter the number of rows which needs to be printed
^
^ ^
^ ^ ^
^ ^ ^ ^
^ ^ ^ ^ ^Demo 类包含 main 函数。在此处,定义了 Scanner 对象,并且从命令行获取所需的行。使用 'row' 的该值,对循环进行迭代。同样,所需的空间数也从命令行获取,并且在打印 “*” 符号之间使用它。“for” 循环再次用于在控制台上以三角形图案打印 “*”。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP