Java程序显示Floyd三角形

王林
王林 转载
2023-09-15 08:05:02 821浏览

Java程序显示Floyd三角形

Floyd的三角形是一个由自然数构成的流行的直角三角形数组。其名称来自于其创始人罗伯特·W·弗洛伊德,他是一位著名的计算机科学家。三角形的顶部是数字1,然后在每一行向下移动时,每个后续数字递增1。

在本文中,我们将看到如何使用Java程序来显示Floyd的三角形。

但在进行Java实现之前,让我们更深入地了解一下弗洛伊德三角形。

第一行只包含一个数字,即1本身,每个后续行比前一行多一个数字。三角形有n行,其中n可以是任何正整数。

The total number of values in the triangle will be the sum of the 1st n natural numbers which is calculated using the formula S = n/2 * (2a + (n-1) d) where S is the sum of the series, n is the number of terms in the series, a is the first term in the series, d is the common difference between the terms.

However, in a Floyd’s triangle, the 1st term is always 1 and the common difference is 1 so we can simplify this formula to:

S= n/2 * (2 + n – 1) = n/2 * (n+1)

因此,Floyd三角形中n行的总值数为n/2 * (n+1)。

如果有5行,即n=5,则三角形中的总值数量为:

5/2 * (5+1) = 15

算法

Input: Number of rows n

  • 1. Initialize a variable "number" to 1

  • 2. For i from 1 to n, do the following −

    • a. 对于从1到i的j,执行以下操作 -

      • i. 打印"number"的值

      • ii. Increment "number" by 1

    • b. 打印一个换行字符以移动到下一行

使用for循环

For loops are a type of control flow statement that executes a set of instructions repeatedly. It contains 3 parts which are an initialization statement, a Boolean condition, and an update statement. After the loop body is executed the update statement is executed and the condition is checked again until the Boolean condition becomes false.

The java implementation to display Floyd’s triangle using nested for loops is given below.

Example

public class FloydTriangle {
   public static void main(String[] args) {
      // declare the number of rows 
      int rows = 5;
      int num = 1;
   
      for (int i = 1; i <= rows; i++) {
         for (int j = 1; j <= i; j++) {
            System.out.print(num + " ");
            num++;
         }
         System.out.println();
      }
   }
}

输出

上述程序将产生以下输出 -

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

Using while loops

While loops are another form of control flow statements which repeatedly execute based up on a pre-defined Boolean condition and terminates itself once the condition is false.

Example

public class FloydTriangle {
   public static void main(String[] args) {
      int rows = 5;
      int number = 1;
      int i = 1;
   
      while (i <= rows) {
         int j = 1;
         while (j <= i) {
            System.out.print(number + " ");
            number++;
            j++;
         }
         System.out.println();
         i++;
      }
   }
}

输出

上述程序将产生以下输出 -

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

使用Do-While循环

do while循环与while循环非常相似,只是它至少执行一次,因为条件在每次迭代结束时被测试。如果条件为真,循环将继续执行,并在条件为假时终止。这里预定义了行数为10。

Example

public class FloydTriangle {
   public static void main(String[] args) {
      int rows = 10;
      int number = 1;
      int i = 1;
   
      do {
         int j = 1;
         do {
            System.out.print(number + " ");
            number++;
            j++;
         } while (j <= i);
         System.out.println();
         i++;
      } while (i <= rows);
   }
}

输出

上述程序将产生以下输出 -

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 
46 47 48 49 50 51 52 53 54 55

结论

弗洛伊德三角形是一个简单的例子,用于演示和练习基本概念,如循环和模式。它不仅限于Java实现,通常用于教授C ++,Java,C#等许多编程语言。三角形由n行组成,其中n可以在编写代码时预定义并存储在整数中。可以进一步优化,要求用户输入n的值或行数(使用Scanner类或任何其他输入方法的帮助),这将给学习者带来更好的练习。总的来说,这个程序是在Java中生成弗洛伊德三角形的一种简单而高效的方法。在定义循环的条件时要注意,以防止进入无限循环。

以上就是Java程序显示Floyd三角形的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除