原文: https://beginnersbook.com/2014/04/java-program-to-print-floyds-triangle-example/
示例程序:
该程序将提示用户输入行数,并根据输入,打印具有相同行数的 Floyd 三角形。
/* Program: It Prints Floyd's triangle based on user inputs* Written by: Chaitanya from beginnersbook.com* Input: Number of rows* 输出: floyd's triangle*/import java.util.Scanner;class FloydTriangleExample{public static void main(String args[]){int rows, number = 1, counter, j;//To get the user's inputScanner input = new Scanner(System.in);System.out.println("Enter the number of rows for floyd's triangle:");//Copying user input into an integer variable named rowsrows = input.nextInt();System.out.println("Floyd's triangle");System.out.println("****************");for ( counter = 1 ; counter <= rows ; counter++ ){for ( j = 1 ; j <= counter ; j++ ){System.out.print(number+" ");//Incrementing the number valuenumber++;}//For new lineSystem.out.println();}}}
输出:
Enter the number of rows for floyd's triangle:6Floyd's triangle****************12 34 5 67 8 9 1011 12 13 14 1516 17 18 19 20 21
