Java 菜单驱动程序执行数组操作

javaserver side programmingprogramming

Java 中的数组是一种非原始数据类型,它存储固定数量的单一类型值。它被称为一维数组。

在本文中,我们将了解如何使用 Java 菜单驱动程序执行不同的数组操作,如检查重复元素、以相反的顺序打印数组、检查最大元素、检查最小元素、查找所有数组元素的总和。我们将使用 switch case 实现应用程序。

向您展示一些实例 -

实例 1

假设我们创建了一个包含 6 个元素的数组,数组元素为 [2,4,6,2,6,8]。
现在我们将找到数组中的重复元素。因此结果将是给定数组中的重复元素:
2
6

实例-2

假设我们创建了一个包含 6 个元素的数组,数组元素为 [2,4,6,2,6,8]。
现在我们将以相反的顺序打印数组。因此结果将是
原始数组:
2 4 6 2 6 8
反向数组:
8 6 2 6 4 2

实例-3

假设我们创建了一个包含 6 个元素的数组,数组元素为 [2,4,6,2,6,8]。现在我们将打印数组中的最大元素。
因此结果将是给定数组中存在的最大元素:8

实例-4

假设我们创建了一个包含 6 个元素的数组,数组元素为 [2,4,6,2,6,8]。现在我们将打印数组中最小的元素。因此结果将是。
给定数组中存在的最小元素:2

实例-5

假设我们创建了一个包含 6 个元素的数组,数组元素为 [2,4,6,2,6,8]。现在我们将打印数组中所有项目的总和。因此结果将是
数组所有元素的总和:28

语法

要在数组中执行基本操作,如查找重复项、反转数组、数组的最大元素、数组的最小元素、打印数组所有元素的总和,我们使用 for 循环,并对上述每个部分使用一些基本逻辑。

以下是"for 循环"的语法 -

for(初始化; 条件; 增量/减量){//语句}

算法

步骤 1 - 要求用户输入所需元素以创建数组。

步骤 2 - 显示菜单。

步骤 3 - 要求用户输入他们的选择。

步骤 4 - 使用开关案例转到选择并执行操作。

步骤 5 - 打印结果。

让我们看一下程序以清楚地理解它。

示例

import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements you want to store: "); int n=sc.nextInt(); int[] array = new int[n]; System.out.println("Enter the elements of the array: "); for(int i=0; i<n; i++) { array[i]=sc.nextInt(); } System.out.println("Array elements are: "); for (int i=0; i<n; i++) { System.out.println(array[i]); } mainLoop: while (true) { Scanner inn = new Scanner( System.in ); System.out.println("\n***Menu***"); System.out.println("1. Find duplicate elements of an array"); System.out.println("2. Print array in reverse order"); System.out.println("3. Print the largest element in an array"); System.out.println("4. Print the smallest element in an array"); System.out.println("5. Print the sum of all the items of the array"); System.out.println("6. Terminate the program"); System.out.println("Enter action number (1-6): "); int command; if ( inn.hasNextInt() ) { command = inn.nextInt(); inn.nextLine(); } else { System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER."); inn.nextLine(); continue; } switch(command) { case 1: System.out.println("Duplicate elements in given array: "); for(int i = 0; i < array.length; i++) { for(int j = i + 1; j < array.length; j++) if(array[i] == array[j]) System.out.println(array[j]); } } break; case 2: System.out.println("Original array: "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); System.out.println("Array in reverse order: "); for (int i = array.length-1; i >= 0; i--) { System.out.print(array[i] + " "); } break; case 3: int max = array[0]; for (int i = 0; i < array.length; i++) { if(array[i] > max) max = array[i]; } System.out.println("Largest element present in given array: " + max); break; case 4: int min = array[0]; for (int i = 0; i < array.length; i++) { if(array[i] <min) min = array[i]; } System.out.println("Smallest element present in given array: " + min); break; case 5: int sum = 0; for (int i = 0; i < array.length; i++) { sum = sum + array[i]; } System.out.println("Sum of all the elements of an array: " + sum); break; case 6: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } } }

输出

Enter the number of elements you want to store: 5
Enter the elements of the array:
4 1 5 3 2
Array elements are:
4
1
5
3
2

***Menu***
1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
2
Original array:
4 1 5 3 2
Array in reverse order:
2 3 5 1 4

***Menu***
1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
1
Duplicate elements in given array:

***Menu***
1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
5
Sum of all the elements of an array: 15

***Menu***
1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
3
Largest element present in given array: 5

***Menu***
1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
4
Smallest element present in given array: 1

***Menu***
1. Find duplicate elements of an array
2. Print array in reverse order
3. Print the largest element in an array
4. Print the smallest element in an array
5. Print the sum of all the items of the array
6. Terminate the program
Enter action number (1-6):
6
Program terminated

在本文中,我们探讨了如何使用菜单驱动方法在 Java 中执行不同的数组操作。


相关文章