Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để tạo kim tự tháp và mô hình

Trong bài này, chúng ta sẽ hiểu làm thế nào để tạo kim tự tháp và mô hình. Mẫu được hình thành bằng cách sử dụng nhiều vòng lặp for và câu lệnh in.

Dưới đây là một minh chứng về điều tương tự -

Đầu vào

Giả sử đầu vào của chúng tôi là -

The number of rows is defined as 8

Đầu ra

Đầu ra mong muốn sẽ là -

The pyramid pattern :
             1
           2 3 2
         3 4 5 4 3
        4 5 6 7 6 5 4
     5 6 7 8 9 8 7 6 5
   6 7 8 9 10 11 10 9 8 7 6
 7 8 9 10 11 12 13 12 11 10 9 8 7
8 9 10 11 12 13 14 15 14 13 12 11 10 9 8

Thuật toán

Step 1 - START
Step 2 - Declare six integer values namely my_input, k, i, my_count, my_temp and my_spaces.
Step 3 - Read the required values from the user/ define the values
Step 4 - Assign value of 0 to ‘k’, my_count and my_temp
Step 5 - We iterate through two nested 'for' loops to get space between the characters.
Step 6 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the value of (i + k - 2 * my_temp).
Step 7 - Now, print a newline to get the specific number of characters in the subsequent lines.
Step 8 - Display the result
Step 9 - Stop

Ví dụ 1

Ở đây, đầu vào đang được người dùng nhập dựa trên lời nhắc. Bạn có thể thử ví dụ này trực tiếp trong công cụ nền tảng mã hóa của chúng tôi Chương trình Java để tạo kim tự tháp và mô hình .

import java.util.Scanner;
public class Pyramid {
   public static void main(String[] args) {
      int my_input, k, i, my_count, my_temp, my_spaces;
      my_input = 8;
      k = 0;
      my_count = 0;
      my_temp = 0;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number of rows: ");
      my_input = my_scanner.nextInt();
      System.out.println("The pyramid pattern : ");
      for ( i = 1; i <= my_input; ++i) {
         for (my_spaces = 1; my_spaces <= my_input - i; ++my_spaces) {
            System.out.print(" ");
            ++my_count;
         }
         while (k != 2 * i - 1) {
            if (my_count <= my_input - 1) {
               System.out.print((i + k) + " ");
               ++my_count;
             } else {
                 ++my_temp;
                 System.out.print((i + k - 2 * my_temp) + " ");
             }
             ++k;
          }
          my_temp = my_count = k = 0;
          System.out.println();
     }
   }
}

Đầu ra

Required packages have been imported
A reader object has been defined
Enter the number of rows: 8
The pyramid pattern :
             1
           2 3 2
         3 4 5 4 3
        4 5 6 7 6 5 4
      5 6 7 8 9 8 7 6 5
    6 7 8 9 10 11 10 9 8 7 6
  7 8 9 10 11 12 13 12 11 10 9 8 7
8 9 10 11 12 13 14 15 14 13 12 11 10 9 8

Ví dụ 2

Ở đây, số nguyên đã được xác định trước đó và giá trị của nó được truy cập và hiển thị trên bảng điều khiển.

public class Pyramid {
   public static void main(String[] args) {
   int my_input, k, i, my_count, my_temp, my_spaces;
      my_input = 8;
      k = 0;
      my_count = 0;
      my_temp = 0;
      System.out.println("The number of rows is defined as " +my_input);
      System.out.println("The pyramid pattern : ");
      for ( i = 1; i <= my_input; ++i) {
         for (my_spaces = 1; my_spaces <= my_input - i; ++my_spaces) {
            System.out.print(" ");
            ++my_count;
         }
         while (k != 2 * i - 1) {
            if (my_count <= my_input - 1) {
               System.out.print((i + k) + " ");
               ++my_count;
            } else {
               ++my_temp;
               System.out.print((i + k - 2 * my_temp) + " ");
            }
            ++k;
          }
          my_temp = my_count = k = 0;
          System.out.println();
      }
   }
}

Đầu ra

The number of rows is defined as 8
The pyramid pattern :
                  1
                2 3 2
             3 4 5 4 3
           4 5 6 7 6 5 4
         5 6 7 8 9 8 7 6 5
     6 7 8 9 10 11 10 9 8 7 6
  7 8 9 10 11 12 13 12 11 10 9 8 7
8 9 10 11 12 13 14 15 14 13 12 11 10 9 8