Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java tạo kim tự tháp và mẫu số

Giới thiệu

Trong bài viết này, chúng ta sẽ tìm hiểu cách tạo kim tự tháp và mẫu số (pyramid and pattern). Mẫu được tạo bằng nhiều vòng lặp for lồng nhau và lệnh in.

Minh họa

Đầu vào:

The number of rows is defined as 8

Đầu ra:

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

  1. Bắt đầu
  2. Khai báo 6 số nguyên: my_input, k, i, my_count, my_temp, my_spaces
  3. Đọc giá trị từ người dùng hoặc định nghĩa trước
  4. Gán 0 cho k, my_count, my_temp
  5. Duyệt 2 vòng lặp for lồng nhau để tạo khoảng trắng
  6. Sau vòng lặp trong cùng, dùng vòng while in giá trị (i + k - 2 * my_temp)
  7. In dòng mới cho các dòng tiếp theo
  8. Hiển thị kết quả
  9. Kết thúc

Ví dụ 1: Nhập từ bàn phím (Scanner)

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: Giá trị định nghĩa trước

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