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

Chương trình Java để in mẫu số xoắn ốc

Trong bài này, chúng ta sẽ hiểu làm thế nào để in mô hình xoắn ốc của số. 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à -

Enter the size : 5

Đầu ra

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

The spiral pattern
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 3 2 2 4 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5

Thuật toán

Step 1 - START
Step 2 - Declare three integer values namely i, my_pattern_size and my_input
Step 3 - Read the required values from the user/ define the values
Step 4 - Assign 2 * my_input – 1 value to my_pattern_size
Step 4 - We iterate through two nested 'for' loops to get space between the characters.
Step 5 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the required character.
Step 6 - Now, print a value of Math.max(Math.abs(i - my_input), Math.abs(j - my_input)) + 1 + in the subsequent lines.
Step 7 - Display the result
Step 8 - 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 để in mẫu số xoắn ốc .

import java.util.Scanner;
import java.lang.Math;
public class SpiralPattern{
   public static void main(String args[]){
      int my_input , i, j,my_pattern_size ;
      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 size : ");
      my_input = my_scanner.nextInt();
      System.out.print("The spiral pattern ");
      System.out.println();
      my_pattern_size = 2 * my_input - 1;
      for( i = 1; i <= my_pattern_size; i++){
         for( j = 1; j <= my_pattern_size; j++){
            System.out.print(Math.max(Math.abs(i - my_input), Math.abs(j - my_input)) + 1 + " ");
         }
         System.out.println();
      }
   }
}

Đầu ra

Required packages have been imported
A reader object has been defined
Enter the size : 5
The spiral pattern
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5

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.

import java.lang.Math;
public class SpiralPattern{
   public static void main(String args[]){
      int my_input , i, j,my_pattern_size ;
      System.out.println("Required packages have been imported");
      my_input = 5;
      System.out.println("The size is defined as " +my_input);
      System.out.print("The spiral pattern ");
      System.out.println();
      my_pattern_size = 2 * my_input - 1;
      for( i = 1; i <= my_pattern_size; i++){
         for( j = 1; j <= my_pattern_size; j++){
            System.out.print(Math.max(Math.abs(i - my_input), Math.abs(j - my_input)) + 1 + " ");
         }
         System.out.println();
      }
   }
}

Đầu ra

Required packages have been imported
The size is defined as 5
The spiral pattern
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5