生成并打印弗洛伊德三角形的 Java 程序


弗洛伊德三角形以罗伯特·弗洛伊德命名,是一个直角三角形,使用自然数制作而成。它从 1 开始,并按顺序选择序列中下一个更大的数字。

算法

  • 取要打印的行数 n。
  • 进行外部迭代 I n 次以打印行
  • 对 J 进行内部迭代以获取 I
  • 打印 K
  • 递增 K
  • 在每次内部迭代后打印换行符

示例

import java.util.Scanner;
public class FloyidsTriangle {
   public static void main(String args[]){
      int n,i,j,k = 1;
      System.out.println("Enter the number of lines you need in the FloyidsTriangle");
      Scanner sc = new Scanner(System.in);
      n = sc.nextInt();

      for(i = 1; i <= n; i++) {
         for(j=1;j <= i; j++){
            System.out.print(" "+k++);
         }
         System.out.println();
      }
   }
}

输出

Enter the number of lines you need in the FloyidsTriangle
9
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45

更新于: 2020-03-13

+4K 次浏览

开启你的 事业

完成课程获取认证

开始
广告
© . All rights reserved.