Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java ตรวจสอบค่าในอาเรย์ (Array Contains Value)


ในบทความนี้ เราจะทำความเข้าใจวิธีตรวจสอบว่า อาเรย์มีค่าที่กำหนดหรือไม่ โดยการวนลูปผ่านแต่ละสมาชิกและเปรียบเทียบกับค่าที่ต้องการค้นหา

ตัวอย่าง Input และ Output

Input:

Enter the number to be searched: 25
The elements in the integer array: 15 20 25 30 35

Output:

The array contains the given value

อัลกอริธึม

ขั้นตอนที่ 1 - เริ่มต้น
ขั้นตอนที่ 2 - ประกาศตัวแปร: my_input, i, array_size, boolean my_check, int array my_array
ขั้นตอนที่ 3 - อ่านค่าจากผู้ใช้ หรือกำหนดค่าเริ่มต้น
ขั้นตอนที่ 4 - ใช้ลูป for วนผ่านอาเรย์ เปรียบเทียบค่ากับ my_input
ขั้นตอนที่ 5 - ถ้าตรงกัน ตั้ง my_check = true และ break
ขั้นตอนที่ 6 - แสดงผลว่าพบหรือไม่พบ
ขั้นตอนที่ 7 - หยุด

ตัวอย่างที่ 1: รับค่าจากผู้ใช้

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 does not contain the given value");
    }
}

Output

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

ตัวอย่างที่ 2: กำหนดค่าไว้ล่วงหน้า

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 does not contain the given value");
    }
}

Output

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