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

Chương trình Java để kiểm tra xem một mảng có chứa giá trị cho trước hay không

Trong bài này, chúng ta sẽ hiểu cách kiểm tra xem một mảng có chứa giá trị đã cho hay không. Điều này được thực hiện bằng cách lặp lại các phần tử của mảng và so sánh đầu vào đã cho với các phần tử của mảng.

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 number to be searched: 25
The elements in the integer array:
15 20 25 30 35

Đầu ra

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

The array contains the given value

Thuật toán

Step 1 - START
Step 2 - Declare three integer values namely my_input , i, array_size. A Boolean value my_check is defined and an integer array my_array is defined
Step 3 - Read the required values from the user/ define the values
Step 4 - Iterate the elements using a for loop and compare the values of the given input with in array values.
Step 5 - If the values match, the element is present. If not, the element is not present.
Step 6 - Display the result
Step 7 - 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 để kiểm tra xem một mảng có chứa giá trị cho trước hay không .

import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
      int my_input , i, array_size;
      boolean my_check = false;
      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.println("Enter the number to be searched ");
      my_input = my_scanner.nextInt();
      System.out.print("Enter the value of array_size : ");
      array_size = my_scanner.nextInt();
      int my_array[] = new int[array_size];
      System.out.println("Enter the elements of the array :" );
      for ( i = 0 ; i < array_size ; i++ ){
         my_array[i] = my_scanner.nextInt();
      }
      for ( i = 0 ; i < array_size ; i++ ) {
         if (my_array[i] == my_input) {
            my_check = true;
            break;
         }
      }
      if(my_check)
         System.out.println("\nThe array contains the given value");
      else
         System.out.println("\nThe array doesnot contain the given value");
   }
}

Đầu ra

Required packages have been imported
A reader object has been defined
Enter the number to be searched
25
Enter the size of array : 5
Enter the elements of the array :
10
15
20
25
30
The array contains the given value

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 Main {
   public static void main(String[] args) {
      int[] my_array = {15, 20, 25, 30, 35, 40};
      int my_input , i, array_size;
      array_size = 5;
      my_input = 25;
      boolean my_check = false;
      System.out.println("The number is defined as " +my_input);
      System.out.println("The elements in the integer array is defined as :" );
      for ( i = 0 ; i < array_size ; i++ ){
         System.out.print(my_array[i] +" ");
      }
      for ( i = 0 ; i < array_size ; i++ ) {
         if (my_array[i] == my_input) {
            my_check = true;
            break;
         }
      }
      if(my_check)
         System.out.println("\nThe array contains the given value");
      else
         System.out.println("\nThe array doesnot contain the given value");
   }
}

Đầu ra

The number is defined as 25
The elements in the integer array is defined as :
15 20 25 30 35
The array contains the given value