在 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